美文网首页
listview同步滑动

listview同步滑动

作者: ffday | 来源:发表于2017-09-07 17:49 被阅读0次

    两个listview同步滚动

    布局就不写了,传入两个需要关联的listview即可实现懂不滑动效果

    
    /**
         * listview同步滑动效果
         * */
        public static void setListViewOnTouchAndScrollListener(final ListView listView1, final ListView listView2){
    
    
            //设置listview2列表的scroll监听,用于滑动过程中左右不同步时校正
            listView2.setOnScrollListener(new AbsListView.OnScrollListener() {
    
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    //如果停止滑动
                    if(scrollState == SCROLL_STATE_IDLE /*|| 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 AbsListView.OnScrollListener() {
    
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    if(scrollState == SCROLL_STATE_IDLE /*|| 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);
    
                    }
                }
            });
        }
    
    

    相关文章

      网友评论

          本文标题:listview同步滑动

          本文链接:https://www.haomeiwen.com/subject/sfaijxtx.html