Saravanan Selvam
Saravanan Selvam

Reputation: 173

How to set the Grid view and View pager inside the scroll view in android?

In my XML i set the entire screen as scrollable, I created the scroll view inside that set the Framelayout for View pager and Grid view. Here Grid view only scrolled but it doesn't scroll the entire screen.

Below is my code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:scrollbars="vertical">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:id="@+id/framelayoutViewpager">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <android.support.v4.view.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
            </android.support.v4.view.ViewPager>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnImagePrevious"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Previous" />

            <Button
                android:id="@+id/btnImageNext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Next" />

        </LinearLayout>

    </FrameLayout>

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/framelayoutViewpager"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:paddingTop="7dp"
        android:layout_marginTop="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginBottom="2dp"
        android:id="@+id/gridView" />

    </RelativeLayout>

    </ScrollView>

</LinearLayout> 

Upvotes: 1

Views: 1411

Answers (2)

Govinda P
Govinda P

Reputation: 3319

If ready to use AppCompact support lib then try NestedScrollView and CoordinatorLayout.

sample layout code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <android.support.v4.view.ViewPager
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:fitsSystemWindows="true"

                app:layout_collapseMode="parallax" />
            <LinearLayout
                android:id="@+id/linearLayout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/btnImagePrevious"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Previous" />

                <Button
                    android:id="@+id/btnImageNext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Next" />

            </LinearLayout>


        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <GridView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/framelayoutViewpager"
            android:numColumns="3"
            android:stretchMode="columnWidth"
            android:paddingTop="7dp"
            android:layout_marginTop="2dp"
            android:layout_marginRight="2dp"
            android:layout_marginBottom="2dp"
            android:id="@+id/gridView" />
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

Upvotes: 0

V-rund Puro-hit
V-rund Puro-hit

Reputation: 5534

NonScrollGridView will do the trick here..

NonScrollGridView

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;

public class NonScrollGridView extends GridViewWithHeaderAndFooter {

   public NonScrollGridView(Context context) {
    super(context);
   }

   public NonScrollGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
   }

   public NonScrollGridView(Context context, AttributeSet attrs, int defStyle)   {
       super(context, attrs, defStyle);
   }

   @Override
   public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
            Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
       super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
       ViewGroup.LayoutParams params = getLayoutParams();
       params.height = getMeasuredHeight();
   }
}

Upvotes: 1

Related Questions