Search code examples
androidgridviewscrollview

How to scroll two gridview and an imageview inside scrollview together


I am having 2 gridview and an imageview inside scrollview, I want to scroll these views together.

The first gridview have only 4 items and second gridview is having more than 4 items.

So is it possible to scroll together.

my layout is,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="170dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

</android.support.v4.view.ViewPager>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="14dp"
    android:src="@drawable/logo_small"
    android:contentDescription="@string/app_name" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/imageView2"
    android:layout_marginRight="14dp"
    android:src="@drawable/next"
    android:contentDescription="@string/app_name" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:layout_marginTop="32dp"
    android:contentDescription="@string/app_name"
    android:src="@drawable/prev" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/viewPager"
    android:layout_centerHorizontal="true"
    android:isScrollContainer="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <GridView
            android:id="@+id/gridView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="2" >
        </GridView>

        <ImageView
            android:id="@+id/iv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/gridView1"
            android:src="@drawable/ads" >
        </ImageView>

        <GridView
            android:id="@+id/gridView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="2"
            android:layout_below="@+id/iv" >
        </GridView>

    </RelativeLayout>
</ScrollView>

</RelativeLayout>

Solution

  • It is not good practise to put one Scrollable view another. As GridView is already scrollable.

    If you want to do that you can refer below links

    1. Problems with GridView inside ScrollView in android
    2. How to put GridView inside ScrollView