android实现两个listview的同步滚动

时间:2022-11-15 19:34:02

在开发的过程中看到项目有实现两个listview同步滚动的效果,所以就希望跟大家分享一下!!

不用说大家可能也想到在listview的onScrollListener中做一些操作,

 

现在具体的细节如下:

public void setListViewOnTouchAndScrollListener(final ListView listView1,final ListView listView2){
         
           
         //设置listview2列表的scroll监听,用于滑动过程中左右不同步时校正
         listView2.setOnScrollListener(new OnScrollListener() {
    
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
     //如果停止滑动
     if(scrollState == 0 || scrollState == 1){
      //获得第一个子view
      View subView = view.getChildAt(0);
      
      if(subView !=null){
       final int top = subView.getTop();
       final int top1 = listView1.getChildAt(0).getTop();
       final int position = view.getFirstVisiblePosition();
       
       //如果两个首个显示的子view高度不等
       if(top != top1){
        listView1.setSelectionFromTop(position, top);
       }
      }
     }
     
    }
    
    public void onScroll(AbsListView view, final int firstVisibleItem,
      int visibleItemCount, int totalItemCount) {
     View subView = view.getChildAt(0);
     if(subView != null){
      final int top = subView.getTop();
 
//      //如果两个首个显示的子view高度不等
      int top1 = listView1.getChildAt(0).getTop();
      if(!(top1 - 7 < top &&top < top1 + 7)){
       listView1.setSelectionFromTop(firstVisibleItem, top);
       listView2.setSelectionFromTop(firstVisibleItem, top);
      }
     
     }
    }
   });
        
       //设置listview1列表的scroll监听,用于滑动过程中左右不同步时校正
         listView1.setOnScrollListener(new OnScrollListener() {
    
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    if(scrollState == 0 || scrollState == 1){
      //获得第一个子view
      View subView = view.getChildAt(0);
      
      if(subView !=null){
       final int top = subView.getTop();
       final int top1 = listView2.getChildAt(0).getTop();
       final int position = view.getFirstVisiblePosition();
       
       //如果两个首个显示的子view高度不等
       if(top != top1){
        listView1.setSelectionFromTop(position, top);
        listView2.setSelectionFromTop(position, top);
       }
      }
     }
    }
    
    @Override
    public void onScroll(AbsListView view, final int firstVisibleItem,
      int visibleItemCount, int totalItemCount) {
     View subView = view.getChildAt(0);
     if(subView != null){
      final int top = subView.getTop();
      listView1.setSelectionFromTop(firstVisibleItem, top);
     listView2.setSelectionFromTop(firstVisibleItem, top);
     
     }
    }
   });
 }