我有一个ViewPager每页显示一个片段.此片段包含RecyclerView中的项目列表.项目列表始终具有相同的大小,并且项目的视图也具有相同的高度.当滚动其中一个RecyclerViews时,我希望其他RecyclerViews同时滚动相同的距离.如何同步RecyclerViews的滚动?
解决方法
这是我的解决方案.代码越少越好……
lvDetail& lvDetail2是您希望保持同步的RecyclerView.
final RecyclerView.OnScrollListener[] scrollListeners = new RecyclerView.OnScrollListener[2]; scrollListeners[0] = new RecyclerView.OnScrollListener( ) { @Override public void onScrolled(RecyclerView recyclerView,int dx,int dy) { super.onScrolled(recyclerView,dx,dy); lvDetail2.removeOnScrollListener(scrollListeners[1]); lvDetail2.scrollBy(dx,dy); lvDetail2.addOnScrollListener(scrollListeners[1]); } }; scrollListeners[1] = new RecyclerView.OnScrollListener( ) { @Override public void onScrolled(RecyclerView recyclerView,dy); lvDetail.removeOnScrollListener(scrollListeners[0]); lvDetail.scrollBy(dx,dy); lvDetail.addOnScrollListener(scrollListeners[0]); } }; lvDetail.addOnScrollListener(scrollListeners[0]); lvDetail2.addOnScrollListener(scrollListeners[1]);