Reputation: 21
I want to scroll multiple RecyclerView
at a time how to achieve that Exp- I have 3 RecyclerView
in horizontal and when i scroll 1st RecyclerView
then second and third shoul also scroll how to do that ?
Upvotes: 2
Views: 1016
Reputation: 2141
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.
Upvotes: 3