Synchronize and scroll multiple ListView views
Introduction:
During development, two or more listview views may be rolled together to maintain the corresponding relationship. This article will show you this effect.
Effect:
Implementation principle:
When you scroll through any of the ListView, you can also set the scroll position of other ListView. The horizontal ListView is used in the example.
The project hosting address of HListView is: https://github.com/sephiroth74/HorizontalVariableListView interested can study
Steps:
1. In order to achieve simultaneous scrolling, there are some conditions. First, the width of the HListView Item is always, as shown in
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="60dp" android:layout_height="60dp" android:gravity="center" android:background="#abcdef" android:text="Terry10" />
2. For the sake of your understanding, I will post the source code as follows. You can focus on the key code:
Public class MainActivity extends Activity {private HListView mListView1; private HListView mListView2; private HListView mListView3; private HListView mListView4; private HListView mListView5; private List <HListView> viewList = new ArrayList <HListView> (); private String [] contentArray = new String [] {"Terry1", "Terry2", "Terry3 ", "Terry4", "Terry5", "Terry6", "Terry7", "Terry8", "Terry9", "Terry10", "Ter Ry11 "," Terry12 "," Terry13 "," Terry14 "," Terry15 "," Terry16 "," Terry17 "," Terry18 "};@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mListView1 = (HListView) findViewById (R. id. listview1); mListView2 = (HListView) findViewById (R. id. listview2); mListView3 = (HListView) findViewById (R. id. listview3); mListView4 = (HList View) findViewById (R. id. listview4); mListView5 = (HListView) findViewById (R. id. listview5); mListView1.setAdapter (new MyAdapter (); mListView2.setAdapter (new MyAdapter (); mListView3.setAdapter (new MyAdapter (); mListView4.setAdapter (new MyAdapter ()); mListView5.setAdapter (new MyAdapter (); viewList. add (mListView1); viewList. add (mListView2); viewList. add (mListView3); viewList. add (mListView4); viewList. add (mLi StView5); MyScrollListener mListener = new MyScrollListener (); for (HListView item: viewList) {item. setOnScrollListener (mListener);} private class MyScrollListener implements OnScrollListener {@ Overridepublic void onScrollStateChanged (AbsHListView view, int scrollState) {// key code if (scrollState = SCROLL_STATE_IDLE | scrollState = SCROLL_STATE_TOUCH_SCROLL) {View subView = view. getChildAt (0); if (su BView! = Null) {final int top = subView. getLeft (); final int position = view. getFirstVisiblePosition (); for (HListView item: viewList) {item. setSelectionFromLeft (position, top) ;}}@ Overridepublic void onScroll (AbsHListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {// key code View subView = view. getChildAt (0); if (subView! = Null) {final int top = subView. getLeft (); for (HListView item: viewList) {item. setSelectionFromLeft (firstVisibleItem, top) ;}}} private class MyAdapter extends BaseAdapter {@ Overridepublic int getCount () {return contentArray. length ;}@ Overridepublic Object getItem (int position) {return contentArray [position] ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, view convertView, ViewGroup parent) {TextView TV = (TextView) getLayoutInflater (). inflate (R. layout. item, parent, false); TV. setText (contentArray [position]); return TV ;}}}
Full text
Android two ListView synchronous scrolling
This implementation can be implemented. I do not recommend you to use this method, but I will give you some ideas to implement the scrolling of another listView IN THE onScroll METHOD OF THE listView you are rolling, however, there is a problem here, which is also a problem that I encountered in my previous project implementation in this way, that is, moving 10px. Here I use LV1 and LV2 to represent two listView lists, when LV1 moves 10 PX up, it must be scaled 10 Px after its Scroll stops. Because in onScroll, if we have to update the rolling status of LV2 without stopping the onScoll status of LV1, that is, the rolling synchronization operation of LV1 and LV2, in this way, we often encounter some problems.
Later, I wanted to roll LV1 and then implement LV2 Rolling Based on the rolling distance. However, the sudden ROLLING OF LV2 is actually not Synchronous Rolling with LV1. In this way, it is not good, whether it is application performance or user experience is poor.
I am not very clear about what is in your ListView, but if you can avoid this, try to avoid it, you can try to use the Layout to put the two ListView items in a Layout and add them to a ScrollView. It seems that the two items can be synchronized and rolled.
Is there a way to synchronize the two ListView views that are side by side?
First, the upstairs method is feasible, but the two lists may have different content lengths and their coordinates are not relative. this still causes problems. you may need to do a lot of control to calculate the step size of the listView sliding by fingers. second. if we use other methods for writing, we use the Scroller class. In fact, the ListView actually has its own Scroller class. you need to customize one. Then, you can use both listView's own one. scroller class. It takes some time for a newbie to study it.