CoordinatorLayout使用ViewPager的RecyclerView

Rod*_*uin 48 android android-appcompat android-recyclerview android-design-library android-coordinatorlayout

我正在使用CoordinatorLayout来自的视图android.support.design.我想附加app:layout_behavior片段的RecyclerView

在Google提供的示例中,它们仅将其附加到附加RecyclerView的相同XML文件中CoordinatorLayout.

有没有办法附加CoordinatorLayout到片段RecyclerViewViewPager

样品是在这个博客帖子在Android开发者博客.

and*_*lic 60

Chris Banes在Github上发布了一个样本,它显示了你想要做什么.

这是xml文件,它定义了如何将协调器布局间接附加到viewpager的片段.

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_done" />

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

我们的想法是让viewpager拥有layout_behavior属性.

  • https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/layout/include_list_viewpager.xml (9认同)
  • 你有github的链接吗? (5认同)
  • 是否可以在viewpager中的任何视图上使用app:layout_behavior?例如,非回收者视图如scrollview? (5认同)

Rod*_*uin 14

这可能是愚蠢的,但它没有奏效,因为构建工具没有在build.gradle应用程序版本中更新为22,我使用21这就是为什么它没有按预期工作的原因.

编辑:

还有什么SanderTuit说:添加com.android.support:recyclerview-v7:22.2.0也将解决问题

  • 我想补充一点,你还需要最新的RecyclerView:com.android.support:recyclerview-v7:22.2.0. (12认同)

Jam*_*Jam 6

使用FrameLayout并将片段注入到FrameLayout中.然后将app:layout_behavior设置为它.您需要做的唯一事情是将layout_behavior设置为AppBayLayout的兄弟,并且兄弟姐妹将在工具栏下方.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

  • 当Framelayout包含回收器视图时,CoordinatorLayout根本不滚动. (4认同)
  • 它经过测试吗?我的意思是我有一个类似的布局,但工具栏不响应我的片段内的viewpager的滚动事件(注入到FrameLayout) (2认同)

小智 6

我最近在帖子中提到了同样的问题,上述解决方案对我不起作用。幸运的是我设法解决了它。所以只是为了分享我在这里发布

问题是在我的代码中我设置了

recyclerView.setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)

因此,尽管将 layout_behaviour 属性设置为 viewPager,appbar 仍没有响应 recyclerView 的滚动。将上述属性更改为

recyclerView.setNestedScrollingEnabled(true);
Run Code Online (Sandbox Code Playgroud)

解决了这个问题,appbar 开始响应 recylerView 的滚动。