Synchronize scrolling on multiple RecyclerViews in adapter

感情迁移 提交于 2019-12-10 15:19:36

问题


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));

回答1:


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




回答2:


It looks like in your case it is better to use horizontal GridLayoutManager.

In this case, there is no need to synchronize scrolling of multiple RecyclerViews.



来源:https://stackoverflow.com/questions/44524485/synchronize-scrolling-on-multiple-recyclerviews-in-adapter

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