【问题标题】:CoordinatorLayout with RecyclerView in different fragmentsCoordinatorLayout 和 RecyclerView 在不同的片段中
【发布时间】:2016-08-03 03:19:11
【问题描述】:

我有一个带有 CoordinatorLayout 和 FrameLayout 的片段来填充其他片段。这些 Fragment 之一包含一个 RecyclerView。我的问题是,RecyclerView 如何与 CoordinatorLayout 一起工作,每个都在一个文件中。我尝试将 NestedScrollView 作为 Fragment 父级,但是当我这样做时,所有元素都会调用适配器 RecyclerView 的“onBindViewHolder”。

Main Fragment 包含 CoordinatorLayout

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/background">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:elevation="0dp"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            android:background="@color/colorTab"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

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

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

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

带有 RecyclerView 的片段

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:background="@color/background"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

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

这样滚动可以工作,但是所有列出的项目都会调用“onBindViewHolder”,包括那些不“可见”的项目。如果我使用 LinearLayout 而不是 NestedScrollView,“onBindViewHolder”会以正确的方式工作,但 CoordinatorLayout 的滚动行为(“@string/appbar_scrolling_view_behavior”)不起作用。

【问题讨论】:

  • 如果你使用NestedScrollView 包裹RecyclerView。回收功能不起作用。而且它会使用更多的内存和延迟。

标签: android android-layout android-fragments android-recyclerview android-coordinatorlayout


【解决方案1】:

ScrollView 绘制它的所有子元素。这意味着您的 Recyclerview 在位于 Scrollview 内时将失去其回收属性。这就是为什么所有项目都调用onBindViewHolder。这也是不正确的行为。

尝试使用滚动行为(“@string/appbar_scrolling_view_behavior”)将父级保持为FrameLayout

【讨论】:

  • 我了解,但是通过 FrameLayout 更改 NestedScrollView 不适用于滚动行为。如果 FrameLayout 与 CoordinatorLayout 位于同一文件中,则它可以工作。
  • 尝试将 FrameLayout(存在于 coordinatorlayout 中)的高度更改为 match_parent
  • 没有用。我认为问题是 NestedScrollView 每当我使用它时,所有项目都加载到 onBindViewHolder 中。我试图设置 NestedScrollView 的高度不起作用。我可以使用另一种布局来代替 NestedScrollView 并且具有滚动行为?
  • 我遇到了类似的问题,并且能够通过RelativeLayout/FrameLayout 获得滚动行为。您可以尝试 RelativeLayout 作为片段的父级,但我怀疑问题出在其他地方。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-29
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
相关资源
最近更新 更多