How to syncronisize two Listview positions

前端 未结 2 561
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 18:49

I have two ListViews. Is there any way to synchronize the position of ListViews when I scroll any one of the Lists. Im implementing an AbsLis

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 19:22

    Here is a working code sample of what nininho is suggesting

    MainActivity

    public class MainActivity extends Activity {
    
    public static String[] Cheeses = new String[] { "Abbaye de Belloc", "Abbaye du Mont des Cats",
            "Abertam", "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
            "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler", "Alverca",
            "Ambert", "American Cheese", };
    
    ListView list1;
    ListView list2;
    
    boolean isLeftListEnabled = true;
    boolean isRightListEnabled = true;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        ListAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
                Cheeses);
    
        list1 = (ListView) findViewById(R.id.list1);
        list1.setAdapter(adapter);
    
        list2 = (ListView) findViewById(R.id.list2);
        list2.setAdapter(adapter);
    
        // IF YOU DO NOT OVERRIDE THIS
        // ONLY THE ONE THAT IS TOUCHED WILL SCROLL OVER
        list1.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        list2.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
    
        list1.setOnScrollListener(new OnScrollListener() {
    
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // onScroll will be called and there will be an infinite loop.
                // That's why i set a boolean value
                if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                    isRightListEnabled = false;
                } else if (scrollState == SCROLL_STATE_IDLE) {
                    isRightListEnabled = true;
                }
            }
    
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                View c = view.getChildAt(0);
                if (c != null && isLeftListEnabled) {
                    list2.setSelectionFromTop(firstVisibleItem, c.getTop());
                }
            }
        });
    
        list2.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                    isLeftListEnabled = false;
                } else if (scrollState == SCROLL_STATE_IDLE) {
                    isLeftListEnabled = true;
                }
            }
    
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                View c = view.getChildAt(0);
                if (c != null && isRightListEnabled) {
                    list1.setSelectionFromTop(firstVisibleItem, c.getTop());
                }
            }
        });
    }
    }
    

    the XML

    
    
    
        
        
    
    

提交回复
热议问题