Skip to content Skip to sidebar Skip to footer

Android: Simultaneously Scrolling Horizontal ListView

Consider I have two Horizontal ListView as shown below: list1 list2 I want to know if it is possible to make the second list (list2) view scroll horizontally simultaneously along

Solution 1:

You can do that - just create such layout and use scroll events:

list1.setOnScrollListener(new OnScrollListener() {
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    // TODO Auto-generated method stub

                }

                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    list2.setSelectionFromTop(firstVisibleItem, list1.getChildAt(0).getTop());
                }
            });

Some explanation:

Better use list.setSelectionFromTop() than list.scrollTo() - because first visible item of first list can be shown partially.

list1.getChildAt(0).getTop() - is construction for getting value of X coordinate of first visible item.


Solution 2:

As There is no HorizontalListView in Android, you must having another adapter view, anyway implement following:

list1.setOnItemSelectedListener(new OnItemSelectedListener()
{
   public void onItemSelected(AdapterView adapterView adapterView, View view, int position, long id){
       list2.setSelection(position);
   }
});

Post a Comment for "Android: Simultaneously Scrolling Horizontal ListView"