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

我有一个使用具有HORIZONTAL方向的linearlayoutmanager的RecyclerView,使用BottomSheet Behavior嵌套在FrameLayout中.

尝试在RecyclerView上垂直拖动时,BottomSheet不响应拖动事件.可能这是因为对于具有水平方向的LayoutManager禁用了垂直滚动.

我已经尝试重写linearlayoutmanager.canScrollVertically()并返回true.这种工作..如果你以非常小心的方式垂直拖动,BottomSheet将响应.但是,只要涉及任何水平移动,BottomSheet就会停止响应垂直拖动事件.

我不确定覆盖canScrollVertically()是否是正确的方法 – 从用户体验的角度来看肯定是不对的.

我还注意到,如果我使用ViewPager而不是带有水平方向LayoutManager的RecyclerView,BottomSheet会根据需要响应垂直滑动事件.

是否还有其他一些LayoutManager,RecyclerView,BottomSheet Behavior或其他方法可以帮助将垂直滚动事件传播到BottomSheet行为?

这里有一个问题的例子:

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

只需展开第一个底页.

解决方法

问题出在哪里?在FrameLayout中.放入CoordinatorLayout时,BottomSheet非常有效.然后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是一个很棒的功能,也是一个非常有趣的移动用户体验设计方法.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...