RecyclerView Sync滚动错误

时间:2017-11-28 12:30:30

标签: java android

我有两个RecyclerView。 其中一个名为“recyclerView”。这是用户触摸的回收器。 当用户向下/向上滚动它时,另一个回收者会跟随它,这个名称为“followerRecView”。

声明

private RecyclerView followerRecView;
private RecyclerView recyclerView;
...
followerRecView.setHasFixedSize(true);
followerRecView.setItemAnimator(null);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(null);

听众:

followerRecView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            followPositionY -= dy;
            Log.d("TODO","FollowPosition "+ followPositionY);
        }
});    
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);    
            recycPositionY -= dy;    
            Log.d("TODO","RecycPosition "+ followPositionY);
            followerRecView.scrollBy(0, dy);
        }
});

日志:

-28 12:06:41.896 15999-15999/com.example.test D/log: log RecycPosition -6
11-28 12:06:41.896 15999-15999/com.example.test D/log: log FollowPosition -6
11-28 12:06:41.956 15999-15999/com.example.test D/log: log RecycPosition -17
11-28 12:06:41.956 15999-15999/com.example.test D/log: log FollowPosition -17
11-28 12:06:42.006 15999-15999/com.example.test D/log: log RecycPosition -26
11-28 12:06:42.016 15999-15999/com.example.test D/log: log FollowPosition -26
11-28 12:06:42.046 15999-15999/com.example.test D/log: log RecycPosition -33
11-28 12:06:42.046 15999-15999/com.example.test D/log: log FollowPosition -33
11-28 12:06:42.086 15999-15999/com.example.test D/log: log RecycPosition -36
11-28 12:06:42.086 15999-15999/com.example.test D/log: log FollowPosition -36
11-28 12:06:42.186 15999-15999/com.example.test D/log: log RecycPosition -48
11-28 12:06:42.186 15999-15999/com.example.test D/log: log FollowPosition -48
11-28 12:06:42.226 15999-15999/com.example.test D/log: log RecycPosition -53
11-28 12:06:42.226 15999-15999/com.example.test D/log: log FollowPosition -53
11-28 12:06:42.266 15999-15999/com.example.test D/log: log RecycPosition -54
11-28 12:06:42.266 15999-15999/com.example.test D/log: log FollowPosition -54
11-28 12:06:42.346 15999-15999/com.example.test D/log: log RecycPosition -59
11-28 12:06:42.346 15999-15999/com.example.test D/log: log FollowPosition -59
11-28 12:06:42.376 15999-15999/com.example.test D/log: log RecycPosition -60
11-28 12:06:42.376 15999-15999/com.example.test D/log: log FollowPosition -60
11-28 12:06:42.406 15999-15999/com.example.test D/log: log RecycPosition -61
11-28 12:06:42.406 15999-15999/com.example.test D/log: log FollowPosition -61
11-28 12:06:42.446 15999-15999/com.example.test D/log: log RecycPosition -63
11-28 12:06:42.446 15999-15999/com.example.test D/log: log FollowPosition -63
11-28 12:06:42.476 15999-15999/com.example.test D/log: log RecycPosition -64
11-28 12:06:42.476 15999-15999/com.example.test D/log: log FollowPosition -64
11-28 12:06:42.506 15999-15999/com.example.test D/log: log RecycPosition -65
11-28 12:06:42.506 15999-15999/com.example.test D/log: log FollowPosition -65
11-28 12:06:44.546 15999-15999/com.example.test D/log: log RecycPosition -82
11-28 12:06:44.546 15999-15999/com.example.test D/log: log FollowPosition -82
11-28 12:06:44.576 15999-15999/com.example.test D/log: log RecycPosition -183
11-28 12:06:44.586 15999-15999/com.example.test D/log: log FollowPosition -183
11-28 12:06:44.586 15999-15999/com.example.test D/log: log RecycPosition -186
11-28 12:06:44.586 15999-15999/com.example.test D/log: log FollowPosition -186
11-28 12:06:44.626 15999-15999/com.example.test D/log: log RecycPosition -303
11-28 12:06:44.626 15999-15999/com.example.test D/log: log FollowPosition -289
11-28 12:06:44.656 15999-15999/com.example.test D/log: log RecycPosition -411
11-28 12:06:44.686 15999-15999/com.example.test D/log: log RecycPosition -289
11-28 12:06:44.686 15999-15999/com.example.test D/log: log FollowPosition -167
11-28 12:06:44.736 15999-15999/com.example.test D/log: log RecycPosition -289
11-28 12:06:44.766 15999-15999/com.example.test D/log: log RecycPosition -289
11-28 12:06:44.796 15999-15999/com.example.test D/log: log RecycPosition -289
11-28 12:06:44.836 15999-15999/com.example.test D/log: log RecycPosition -289
11-28 12:06:44.866 15999-15999/com.example.test D/log: log RecycPosition -289
11-28 12:06:44.906 15999-15999/com.example.test D/log: log RecycPosition -289
11-28 12:06:44.936 15999-15999/com.example.test D/log: log RecycPosition -289

错误发生在这里:

11-28 12:06:44.586 15999-15999/com.example.test D/log: log RecycPosition -186
11-28 12:06:44.586 15999-15999/com.example.test D/log: log FollowPosition -186
11-28 12:06:44.626 15999-15999/com.example.test D/log: log RecycPosition -303
11-28 12:06:44.626 15999-15999/com.example.test D/log: log FollowPosition -289
11-28 12:06:44.656 15999-15999/com.example.test D/log: log RecycPosition -411
11-28 12:06:44.686 15999-15999/com.example.test D/log: log RecycPosition -289
11-28 12:06:44.686 15999-15999/com.example.test D/log: log FollowPosition -167
11-28 12:06:44.736 15999-15999/com.example.test D/log: log RecycPosition -289

enter image description here 当我扔掉“recyclerView”时就会发生这种情况 有人有想法吗?谢谢

1 个答案:

答案 0 :(得分:0)

我找到了这个解决方案。

   followerRecView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                followPositionY -= dy;
                Log.d("TODO","FollowPosition "+ followPositionY);
            }
    });    


recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);    
            recycPositionY -= dy;    
            Log.d("TODO","RecycPosition "+ followPositionY);
            followerRecView.scrollBy(0, dy);

            int differenceY = recycPositionY - followPositionY;
            if(differenceY != 0){
                followerRecView.scrollBy(0, -differenceY);
            }
        }
});