I want to implement an horizontal RecyclerView inside a vertical RecyclerView.
The end result should be like this:
So, for each element in the vertical RecyclerView, I need another one in an horizontal way. Kind of like a school schedule, with the Day in the left, and the actual schedule on the right, scroll-able horizontally.
I managed to achieve this, by putting an RecyclerView inside of the first RecyclerView item. Everything works perfectly, but all the horizontal RecyclerViews are scrolling separately. What I want to do is make all the horizontal RecyclerViews to sync and scroll at the same time. How can I achieve this?
The way I set the adapter and the horizontal RecyclerView inside the onBindViewHolder method of the vertical adapter is this:
scheduleAdapter = new ScheduleAdapter(context, data);
holder.scheduleRecyclerView.setAdapter(scheduleAdapter);
holder.scheduleRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
Each recyclerview should add the below scroll listener
.
m_jParentRecyclerViewLayoutManager
is the parent RecyclerView whose items has a recyclerview.
RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
scrollAllRecyclerView(recyclerView, dx, dy);
}
private void scrollAllRecyclerView(RecyclerView recyclerView, int dx, int dy) {
// Scroll children RecyclerViews except the recyclerView that is listened.
for (int i = 0; i < m_jParentRecyclerViewLayoutManager.getChildCount(); i++) {
RecyclerView child = (RecyclerView) m_jParentRecyclerViewLayoutManager.getChildAt(i);
if (child != recyclerView) {
scroll(child, dx, dy);
}
}
}
}
private void scroll(RecyclerView recyclerView, int dx, int dy) {
recyclerView.removeOnScrollListener(this);
recyclerView.scrollBy(dx, dy);
recyclerView.addOnScrollListener(this);
}
EDIT : In your parent recyclerview adapter.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView recyclerView = new RecyclerView(mContext);
.. set layout manager & your adapter .
if (scrollListener != null) {
recyclerView.removeOnScrollListener(scrollListener );
recyclerView.addOnScrollListener(scrollListener );
}
return new RecyclerViewViewHolder(recyclerView);
}
EDIT 2 : There is an Table View library which scroll all child recyclerviews sync. You can check the source code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With