I want to scroll multiple recyclerview at a time how to achieve that

不打扰是莪最后的温柔 提交于 2019-12-06 15:52:18

The answer is very simple, you have to get scroll feedback from one recycleview and pass it to other recycleviews. But very carefully.

you need to keep referance of which recyclerview starts giving scroll feedback, so that it won't enter into continious loop.

so create a global variable

private int draggingView = -1;

Add scrollListener to all your recyclerviews

mRecyclerView1.addOnScrollListener(scrollListener);
mRecyclerView2.addOnScrollListener(scrollListener);
mRecyclerView3.addOnScrollListener(scrollListener);

your scroll listener should be in this structure. It should decide which recyclerview is giving scroll input and which is receiving.

private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (mRecyclerView1 == recyclerView && newState == RecyclerView.SCROLL_STATE_DRAGGING) {
            draggingView = 1;
        } else if (mRecyclerView2 == recyclerView && newState == RecyclerView.SCROLL_STATE_DRAGGING) {
            draggingView = 2;
        } else if (mRecyclerView3 == recyclerView && newState == RecyclerView.SCROLL_STATE_DRAGGING) {
            draggingView = 3;
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (draggingView == 1 && recyclerView == mRecyclerView1) {
            mRecyclerView2.scrollBy(dx, dy);
        } else if (draggingView == 2 && recyclerView == mRecyclerView2) {
            mRecyclerView1.scrollBy(dx, dy);
        } else if (draggingView == 3 && recyclerView == mRecyclerView3) {
            mRecyclerView1.scrollBy(dx, dy);
        }
    }
};

Thats all, your recyclerview is ready to scroll. If you scroll one recycleview, it will scroll all other recyclerviews.

Moving is not a problem, stopping is. If user flinged a list, at the same time if he stopped other list, that list will only be stopped, So you need to cover that case too. I hope this explanation and code will solve your problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!