开发者

Synchronize two ListView positions

开发者 https://www.devze.com 2023-01-26 17:08 出处:网络
I have two Lis开发者_开发知识库tViews. Is there any way to synchronize the position of ListViews when I scroll any one of theListsImplement an AbsListView.OnScrollListener, register to the ListView. W

I have two Lis开发者_开发知识库tViews. Is there any way to synchronize the position of ListViews when I scroll any one of the Lists


Implement an AbsListView.OnScrollListener, register to the ListView. When the ListView is scrolled, the onScroll() method of OnScrollListener will be triggered, then call smoothScrollToPosition() on another ListView to synchronize to the same position.


We did this and it worked well for us.

listOne.setOnTouchListener(new OnTouchListener() {
    @Override
       public boolean onTouch(View arg0, MotionEvent arg1) {
       listTwo.dispatchTouchEvent(arg1);
       return false;
    }
});
listOne.setOnScrollListener(new OnScrollListener() {
   @Override
   public void onScrollStateChanged(AbsListView arg0, int arg1) {
   }
   @Override
   public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
          if (l1.getChildAt(0) != null) {
              Rect r = new Rect();
              l1.getChildVisibleRect(l1.getChildAt(0), r, null);
              l2.setSelectionFromTop(l1.getFirstVisiblePosition(), r.top);
          }
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号