【发布时间】: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