我想一次滚动多个recyclerview如何实现 [英] I want to scroll multiple recyclerview at a time how to achieve that

查看:136
本文介绍了我想一次滚动多个recyclerview如何实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一次滚动多个RecyclerView来实现Exp-我在水平方向上有3个RecyclerView,当我滚动第一个RecyclerView时,第二个和第三个应该滚动该怎么做?

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 ?

推荐答案

答案很简单,您必须从一个recycleview获取滚动反馈并将其传递给其他recycleview.但是要非常小心.

The answer is very simple, you have to get scroll feedback from one recycleview and pass it to other recycleviews. But very carefully.

您需要参考哪个recyclerview开始提供滚动反馈,以便它不会进入连续循环.

you need to keep referance of which recyclerview starts giving scroll feedback, so that it won't enter into continious loop.

因此创建一个全局变量

private int draggingView = -1;

将scrollListener添加到您的所有回收站视图中

Add scrollListener to all your recyclerviews

mRecyclerView1.addOnScrollListener(scrollListener);
mRecyclerView2.addOnScrollListener(scrollListener);
mRecyclerView3.addOnScrollListener(scrollListener);

您的滚动侦听器应位于此结构中.它应该确定哪个recyclerview提供滚动输入,哪个正在接收滚动.

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

仅此而已,您的recyclerview就可以滚动了.如果滚动一个回收站视图,它将滚动所有其他回收站视图.

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.

这篇关于我想一次滚动多个recyclerview如何实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆