Search code examples
androidandroid-layoutscrollviewandroid-gridview

How to do synchronous scrolling for ten gridView in one LinearLayout


I have two gridView the same size. How to do synchronous scrolling. Could I disable gridView scrolling and enable LinearLayout scrolling?

<LinearLayout
     android:layout_width="1080dp"
     android:layout_height="700dp"
     android:layout_alignParentTop="true"
     android:orientation="horizontal"
     android:layout_marginLeft="0dp"
         android:layout_marginTop="0dp"
     >
     <GridView
         android:id="@+id/gridView1"
         android:layout_width="60dp"
         android:layout_height="match_parent"
         android:numColumns="1"
         >
     </GridView>
     <GridView
         android:id="@+id/gridView2"
         android:layout_width="240dp"
         android:layout_height="match_parent"
         android:numColumns="1" >
     </GridView>
</LinearLayout>

Solution

  • Could I disable gridView scrolling and enable LinearLayout scrolling?
    

    for this question.No.there is another more effective way.

    GridView is provided the smoothScrollToPosition(int) or setSelection(int) method via its AbsLisView parent. This will scroll to the position you desire. Now, you should listen for ScrollEvent with the OnScrollListener.

    Now just add these code together:

    GridView other;
    @Override public void onScroll(AbsListView view, int firstItem, int visItems, int total) {
       other.smoothScrollToPosition(firstItem);
    }
    

    Hope it will help.