在适配器中的多个RecyclerViews上同步滚动

在适配器中的多个RecyclerViews上同步滚动

本文介绍了在适配器中的多个RecyclerViews上同步滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在垂直的 RecyclerView 中实现水平的 RecyclerView .

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

最终结果应该是这样的:

The end result should be like this:

因此,对于垂直的 RecyclerView 中的每个元素,我都需要水平放置另一个元素.有点像学校的时间表,左侧是日",右侧是实际的时间表,可水平滚动.

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.

我设法通过在第一个 RecyclerView 项目内放置一个 RecyclerView 来实现这一目标.一切正常,但所有水平 RecyclerView 都在单独滚动.我想做的是使所有水平 RecyclerView 同步并同时滚动.我该如何实现?

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?

我在垂直适配器的 onBindViewHolder 方法中设置适配器和水平 RecyclerView 的方式是:

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

推荐答案

每个recyclerview应该添加以下scroll listener.

Each recyclerview should add the below scroll listener.

m_jParentRecyclerViewLayoutManager是父级RecyclerView,其项目具有recyclerview.

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

:在您的父recyclerview适配器中.

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

编辑2 :有一个表视图库,它会全部滚动子recyclerviews同步.您可以查看源代码

EDIT 2 : There is an Table View library which scroll all child recyclerviews sync.You can check the source code

这篇关于在适配器中的多个RecyclerViews上同步滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 10:49