RecyclerView(水平)嵌套在BottomSheet中,防止垂直滚动

时间:2017-05-09 13:25:59

标签: android android-recyclerview androiddesignsupport bottom-sheet

我有一个RecyclerView使用LinearLayoutManager HORIZONTAL方向,使用FrameLayout BottomSheet嵌套在Behavior内。

尝试在RecyclerView上垂直拖动时,BottomSheet不响应拖动事件。大概这是因为水平方向的LayoutManager禁用了垂直滚动。

我已尝试覆盖LinearLayoutManager.canScrollVertically()并返回true。这种有效。如果你以非常谨慎的方式垂直拖动,BottomSheet会响应。但是,只要涉及任何水平移动,BottomSheet就会停止响应垂直拖动事件。

我不确定覆盖canScrollVertically()是否是正确的方法 - 从用户体验的角度来看,它确实感觉不对。

我还注意到,如果我使用ViewPager而不是RecyclerView并使用横向LayoutManager,则BottomSheet会根据需要响应垂直滑动事件。

是否有LayoutManagerRecyclerViewBottomSheet Behavior或其他完全可以帮助将垂直滚动事件传播到BottomSheet Behavior的其他方法?

这里有一个问题的例子:

https://github.com/timusus/bottomsheet-test (问题可以通过提交#f59a7031重现)

只需展开第一张底页。

1 个答案:

答案 0 :(得分:20)

问题出在哪里?在FrameLayout。放入BottomSheetCoordinatorLayout完美无缺。然后BottomSheet可以将其滚动状态通过CoordinatorLayout传递给作为CoordinatorLayout的直接子项的其他视图。

为什么RecyclerView无法将滚动状态传递给BottomSheet?它不是CoordinatorLayout的直接孩子。但是有一种方法可以传递它们:RecyclerView必须放在视图中,以实现NestedScrollingParentNestedScrollingChild。答案是:NestedScrollView

因此,您的fragment_sheetX.xml布局应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical"
    android:fillViewport="true">

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

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

另请注意android:fillViewport="true",否则您的RecyclerView将无法占据整个身高。

然而它仍然无效。为什么?必须告诉RecyclerView将垂直滚动传递给父级。怎么样?答案是recyclerView.setNestedScrollingEnabled(false);,但更好地描述了here

顺便说一句:MultiSheetView是移植用户体验设计的一个很棒的功能和非常有趣的方法。