同步滚动多个RecyclerViews

时间:2015-06-08 06:48:55

标签: android android-recyclerview

我有一个ViewPager,每页显示一个片段。此片段包含RecyclerView中的项目列表。项目列表始终具有相同的大小,并且项目的视图也具有相同的高度。当滚动其中一个RecyclerViews时,我希望其他RecyclerViews同时滚动相同的距离。我如何同步RecyclerViews的滚动?

8 个答案:

答案 0 :(得分:17)

这是我的解决方案。代码越少越好......

lvDetail& lvDetail2是您希望保持同步的RecyclerView。

    final RecyclerView.OnScrollListener[] scrollListeners = new RecyclerView.OnScrollListener[2];
    scrollListeners[0] = new RecyclerView.OnScrollListener( )
    {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy)
        {
            super.onScrolled(recyclerView, dx, dy);
            lvDetail2.removeOnScrollListener(scrollListeners[1]);
            lvDetail2.scrollBy(dx, dy);
            lvDetail2.addOnScrollListener(scrollListeners[1]);
        }
    };
    scrollListeners[1] = new RecyclerView.OnScrollListener( )
    {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy)
        {
            super.onScrolled(recyclerView, dx, dy);
            lvDetail.removeOnScrollListener(scrollListeners[0]);
            lvDetail.scrollBy(dx, dy);
            lvDetail.addOnScrollListener(scrollListeners[0]);
        }
    };
    lvDetail.addOnScrollListener(scrollListeners[0]);
    lvDetail2.addOnScrollListener(scrollListeners[1]);

答案 1 :(得分:16)

我认为理解它的工作原理是相关的,所以我将解释我设计解决方案时所遵循的整个过程。请注意,此示例仅适用于两个RecyclerViews,但使用更多的RecyclerViews就像使用RecyclerViews数组一样简单。

首先想到的选项是在两个ScrollViews上监听滚动更改,当其中一个滚动时,在另一个上使用scrollBy(int x, int y)。不幸的是,以编程方式滚动也会触发监听器,因此您将以循环结束。

要解决此问题,您需要设置一个OnItemTouchListener,在触摸RecyclerView时添加正确的ScrollListener,并在滚动停止时将其删除。这几乎完美无缺,但是如果你在一个很长的RecyclerView中快速执行,然后在它完成之前再次滚动它,那么只会传输第一个滚动。

要解决此问题,您需要确保仅在RecyclerView空闲时添加OnScrollListener。

让我们来看看来源:

    public class SelfRemovingOnScrollListener extends RecyclerView.OnScrollListener {

    @Override
    public final void onScrollStateChanged(@NonNull final RecyclerView recyclerView, final int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            recyclerView.removeOnScrollListener(this);
        }
    }
}

这是您需要扩展OnScrollListeners的类。这可确保在需要时将其删除。

然后我有两个侦听器,每个RecyclerView一个:

private final RecyclerView.OnScrollListener mLeftOSL = new SelfRemovingOnScrollListener() {
    @Override
    public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
        super.onScrolled(recyclerView, dx, dy);
        mRightRecyclerView.scrollBy(dx, dy);
    }
}, mRightOSL = new SelfRemovingOnScrollListener() {

    @Override
    public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
        super.onScrolled(recyclerView, dx, dy);
        mLeftRecyclerView.scrollBy(dx, dy);
    }
};

然后在初始化时,您可以设置OnItemTouchListeners。最好为整个视图设置一个监听器,但RecyclerView不支持这个。 OnItemTouchListeners无论如何也不会出现问题:

    mLeftRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

        private int mLastY;

        @Override
        public boolean onInterceptTouchEvent(@NonNull final RecyclerView rv, @NonNull final
        MotionEvent e) {
            Log.d("debug", "LEFT: onInterceptTouchEvent");

            final Boolean ret = rv.getScrollState() != RecyclerView.SCROLL_STATE_IDLE;
            if (!ret) {
                onTouchEvent(rv, e);
            }
            return Boolean.FALSE;
        }

        @Override
        public void onTouchEvent(@NonNull final RecyclerView rv, @NonNull final MotionEvent e) {
            Log.d("debug", "LEFT: onTouchEvent");

            final int action;
            if ((action = e.getAction()) == MotionEvent.ACTION_DOWN && mRightRecyclerView
                    .getScrollState() == RecyclerView.SCROLL_STATE_IDLE) {
                mLastY = rv.getScrollY();
                rv.addOnScrollListener(mLeftOSL);
            }
            else {
                if (action == MotionEvent.ACTION_UP && rv.getScrollY() == mLastY) {
                    rv.removeOnScrollListener(mLeftOSL);
                }
            }
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(final boolean disallowIntercept) {
            Log.d("debug", "LEFT: onRequestDisallowInterceptTouchEvent");
        }
    });

    mRightRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

        private int mLastY;

        @Override
        public boolean onInterceptTouchEvent(@NonNull final RecyclerView rv, @NonNull final
        MotionEvent e) {
            Log.d("debug", "RIGHT: onInterceptTouchEvent");

            final Boolean ret = rv.getScrollState() != RecyclerView.SCROLL_STATE_IDLE;
            if (!ret) {
                onTouchEvent(rv, e);
            }
            return Boolean.FALSE;
        }

        @Override
        public void onTouchEvent(@NonNull final RecyclerView rv, @NonNull final MotionEvent e) {
            Log.d("debug", "RIGHT: onTouchEvent");

            final int action;
            if ((action = e.getAction()) == MotionEvent.ACTION_DOWN && mLeftRecyclerView
                    .getScrollState
                            () == RecyclerView.SCROLL_STATE_IDLE) {
                mLastY = rv.getScrollY();
                rv.addOnScrollListener(mRightOSL);
            }
            else {
                if (action == MotionEvent.ACTION_UP && rv.getScrollY() == mLastY) {
                    rv.removeOnScrollListener(mRightOSL);
                }
            }
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(final boolean disallowIntercept) {
            Log.d("debug", "RIGHT: onRequestDisallowInterceptTouchEvent");
        }
    });
}

另请注意,在我的特定情况下,RecyclerViews不是第一个接收触摸事件的人,所以我需要拦截它。如果不是这种情况,您可以(应该)将onInterceptTouchEvent(...)中的代码合并到onTouchEvent(...)中。

最后,如果您的用户尝试同时滚动两个RecyclerViews,这将导致崩溃。这里尽力而为的质量解决方案是在包含RecyclerViews的直接父级中设置android:splitMotionEvents="false"

您可以使用此代码here查看示例。

答案 2 :(得分:4)

我想我找到了一个非常容易和简短的答案。

正如JorgeAntonioDíaz-Benito所说:“第一个想到的选择是在ScrollViews上监听滚动更改,当其中一个滚动时,在另一个上使用scrollBy(int x,int y)。不幸的是,以编程方式滚动也会触发侦听器,因此您将最终处于循环中。“

所以你需要解决这个问题。如果您只是跟踪谁在滚动它们将不会循环。

<强>解

public class SelfScrolListener extends RecyclerView.OnScrollListener {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            viewIsScrolling = -1;
        }
    }
}

这是您自定义的OnScrollListener,用于检查scrollState是否为IDLE。这是真的 - &gt;没有人在滚动。所以`int viewIsScolling = -1

现在您需要检测是否可以滚动。 这是代码:

int viewIsScrolling = 1;
boolean firstIsTouched = false;
boolean secondIsTouched = false;
SelfScrolListener firstOSL= new SelfScrolListener() {
    @Override
    public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
        super.onScrolled(recyclerView, dx, dy);

        if (firstIsTouched) {
             if (viewIsScrolling == -1) {
                 viewIsScrolling = 0;
             }
             if (viewIsScrolling == 0) {
                 secondRecyclerView.scrollBy(dx, dy);
             }
        }
    }
};
SelfScrolListener secondOSL= new SelfScrolListener() {
    @Override
    public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if(secondIsTouched){
             if (viewIsScrolling == -1) {
                 viewIsScrolling = 1;
             }
             if (viewIsScrolling == 1) {
                 firstRecyclerView.scrollBy(dx, dy);
             }
        }
    }
};
firstRecyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        firstIsTouched= true;
        return false;
    }
});
secondRecyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        secondIsTouched= true;
        return false;
    }
});
firstRecyclerView.addOnScrollListener(firstOSL);
secondRecyclerView.addOnScrollListener(secondOSL);

viewIsScrolling =一个全局int并在开头设置为-1;没有人滚动的状态。您可以根据需要添加尽可能多的回收视图。

答案 3 :(得分:0)

在您的寻呼机适配器中创建变量,该变量将负责保存其网页的当前scrollY位置,并且只要您getItem(position)将调用更新ListView,就可以查看CacheFragmentStatePagerAdapterhttps://github.com/ksoichiro/Android-ObservableScrollView/blob/master/samples/src/main/java/com/github/ksoichiro/android/observablescrollview/samples/ViewPagerTabActivity.java

答案 4 :(得分:0)

首先考虑仅将NestedScrollView用作RecyclerViews的父对象。这可能需要一些其他调整,但总体思路是相同的:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.v4.widget.NestedScrollView>

如果由于某些原因而无法执行此操作,则可以以编程方式同步滚动。就像其他答案中已经提到的那样,只需要避免onScrolled()事件回调的无限循环即可。换句话说,当您开始以编程方式滚动时,在onScroll()内不进行任何操作,直到以编程方式完成滚动为止。并且不要以编程方式滚动最初滚动的RecyclerView

将所有回收站放入同步

List<RecyclerView> syncRecyclers

致电

addSyncListeners()

享受

public class SyncScrollActivity extends AppCompatActivity {
    private List<RecyclerView> syncRecyclers;
    private boolean isProgrammaticallyScrolling = false;


    private void addSyncListeners() {
        for (RecyclerView recyclerView : syncRecyclers) {
            recyclerView.addOnScrollListener(new SyncOnScrollListener());
        }
    }

    private class SyncOnScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            if (!isProgrammaticallyScrolling) {
                isProgrammaticallyScrolling = true;
                scrollAll(recyclerView, dx, dy);
                isProgrammaticallyScrolling = false;
            }
        }
    }

    private void scrollAll(RecyclerView exceptRecycler, int dx, int dy) {
        for (RecyclerView recyclerView : syncRecyclers) {
            if (!recyclerView.equals(exceptRecycler)) {
                recyclerView.scrollBy(dx, dy);
            }
        }
    }
}

答案 5 :(得分:0)

kotlin版本,此版本无法处理fling(尚未)

for test in test_suite:
    print(test.test_cases.all())

答案 6 :(得分:0)

此解决方案可处理猛击。演示项目here

假设您有三个水平的RecyclerViews。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv0"
        android:layout_width="match_parent"
        android:layout_height="@dimen/itemSize"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:orientation="horizontal"
        app:layout_constraintTop_toTopOf="parent"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv1"
        android:layout_width="match_parent"
        android:layout_height="@dimen/itemSize"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:orientation="horizontal"
        android:layout_marginTop="5dp"
        app:layout_constraintTop_toBottomOf="@id/rv0"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv2"
        android:layout_width="match_parent"
        android:layout_height="@dimen/itemSize"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:orientation="horizontal"
        android:layout_marginTop="5dp"
        app:layout_constraintTop_toBottomOf="@id/rv1"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
  1. 将标签添加到每个RecyclerView。例如:
val rvs = arrayListOf(rv0, rv1, rv2)

for (i in 0 until rvs.size) {
    rvs[i].tag = i
}
  1. 将相同的OnItemTouchListener添加到每个RecyclerView,以了解哪个RecyclerView是活动的。如果已经有一个活动的RecyclerView,则在其上调用stopScroll(如果活动的RecyclerView处于清除状态,它将防止不同步)
var touchedRVTag = -1

val itemTouchListener = object : RecyclerView.OnItemTouchListener {
    override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}

    override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean 
    {
        val rvTag = rv.tag as Int


        if (touchedRVTag != -1 && touchedRVTag != rvTag) {
            rvs[touchedRVTag].stopScroll()
        }

        touchedRVTag = rvTag

        return false
    }

    override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
}

for (i in 0 until rvs.size) {
    rvs[i].addOnItemTouchListener(itemTouchListener)
}
  1. 将相同的OnScrollListener添加到每个RecyclerView中以滚动其他RecyclerViews
val scrollListener = object : RecyclerView.OnScrollListener() {
    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        val currentRVTag = recyclerView.tag as Int

        if (currentRVTag == touchedRVTag) {
            for (rvTag in 0 until rvs.size) {
                if (rvTag != currentRVTag) {
                    rvs[rvTag].scrollBy(dx, 0)
                }
            }
        }
    }
}

for (i in 0 until rvs.size) {
    rvs[i].addOnScrollListener(scrollListener)
}

PS:如果要滚动任何RecyclerView,请以编程方式设置其标签,然后滚动:

touchedRVTag = rv0.tag as Int
rv0.scrollBy(dx, 0)

答案 7 :(得分:0)

我发现了一个非常简单的解决方案,没有任何标志,而且非常动态

第一件事是您必须创建RecyclerView.OnScrollListener并将该侦听器订阅到所需的每个水平布局。 RecyclerView.OnScrollListener

 val scrollListener = object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)
                    horizontalScroll(this, recyclerView, dx)
                }
            }



fun horizontalScroll(
            scrollListener: RecyclerView.OnScrollListener,
            currentRecycler: RecyclerView,
            dx: Int
        ) {
            for (i in 0..parentRecyclerView.childCount) {
                val childRecyclerView =
                    parentRecyclerView.getChildAt(i)?.findViewById<RecyclerView>(R.id.recyclerView)
                if (currentRecycler != childRecyclerView) {
                    childRecyclerView?.removeOnScrollListener(scrollListener)
                    childRecyclerView?.scrollBy(dx, 0)
                    childRecyclerView?.addOnScrollListener(scrollListener)
                }
            }
        }

请注意,您必须具有相同的layout.id的RecyclerView。在具有水平行的垂直RecyclerView的情况下很有用