使用水平滚动嵌套的RecyclerView中的滚动行为

Ant*_*nko 29 xml android material-design android-toolbar android-coordinatorlayout

我必须在每个项目中创建RecyclerView具有嵌套水平的垂直RecyclerView.一切都在CoordinatorLayout.当我通过点击外部嵌套的RecyclerView工具栏隐藏滚动时,但当我通过点击嵌套的一个工具栏滚动父回收器时保持.

任何帮助,将不胜感激.

这是我的xml布局:

main_activity.xml:

<android.support.design.widget.CoordinatorLayout 
   ...>

<FrameLayout
    android:id="@+id/fragment_frame"
    ...
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    ...
    android:fitsSystemWindows="true"
    android:id="@+id/appbar_layout">

        <include layout="@layout/toolbar"/>

</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

这是toolbar.xml:

<android.support.v7.widget.Toolbar
    android:id="@+id/main_toolbar"
    ...
    android:fitsSystemWindows="true"
    app:layout_scrollFlags="scroll|enterAlways">

    <TextView .../>

</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)

fragment.xml之:

<android.support.v7.widget.RecyclerView
    ...
    android:scrollbars="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
Run Code Online (Sandbox Code Playgroud)

而recycleler_view_item.xml:

<RelativeLayout ...>

    <TextView .../>

    <!-- fixme(CullyCross) fix bug with hiding toolbar -->
    <android.support.v7.widget.RecyclerView
        ...
        android:scrollbars="horizontal"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

谢谢,
安东

Lan*_*lot 57

根据我的要求,到目前为止我找到了足够好的解决方案:

在我的情况下,我有一个nestedScrollView4 RecyclerView秒设置水平滚动内部.对于这些中RecyclerView的每一个,我都是以编程方式完成的:

restaurantsRecylerView.setHasFixedSize(true); 
restaurantsRecylerView.setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)

你可能不想要fixedSize,不确定它是否会有任何区别,我的列表总是25,所以我可以用它来表现.完成此操作后,即使我触摸recyclerViews,我也可以毫无问题地滚动

希望能帮助到你

  • setNestedScrollingEnabled 对我有帮助。并不是说您也可以使用 android:nestedScrollingEnabled="false" 在 xml 中设置它:) (2认同)

Pri*_*tel 20

尝试用RecyclerView里面android.support.v4.widget.NestedScrollView.

<android.support.v4.widget.NestedScrollView
        android:id="@+id/nScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<!-- Set other views of your Layout -->

</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

可以Toolbar和中尝试使用不同的layout_scrollFlags

RecylerView.setNestedScrollingEnabled(false); // set it true or false as per requirement
Run Code Online (Sandbox Code Playgroud)

  • 嗨@AntonShkurenko这就是我所做的,我对目前的解决方案感到满意.在我的情况下,我有一个嵌套的循环视图,其中4个RecyclerViews设置为水平滚动内部.对于每个RecyclerViews,我都是以编程方式完成的:restaurantsRecylerView.setHasFixedSize(true); restaurantsRecylerView.setNestedScrollingEnabled(假); 你可能不想要fixedSize,不确定它是否会有任何区别,我的列表总是25,所以我可以用它来表现.完成此操作后,即使我触摸recyclerViews,我也可以毫无问题地滚动 (3认同)
  • 嗨@DanielJulio我刚刚发布了答案,我仍然试图让它更好,因为水平滚动有时候有点困难.谢谢你的评论. (2认同)