Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronize scrolling on multiple RecyclerViews in adapter

Tags:

I want to implement an horizontal RecyclerView inside a vertical RecyclerView.

The end result should be like this:

enter image description here

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));
like image 977
Phantom Avatar asked Jun 13 '17 14:06

Phantom


1 Answers

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

like image 62
ziLk Avatar answered Oct 11 '22 13:10

ziLk