0

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

主要碎片包含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”)不起作用。

+0

如果你使用'NestedScrollView'包'RecyclerView'。回收功能将不起作用。它会使用更多的内存和滞后。 – wonsuc

回答

0

ScrollView绘制所有它的子元素。这意味着当您的RecyclerviewScrollview中时,它会丢失它的回收利用属性。这就是为什么onBindViewHolder被称为所有项目。这也是不正确的行为。

试着保持父为FrameLayout与滚动行为( “@字符串/ appbar_scrolling_view_behavior”)

+0

我明白,但通过FrameLayout更改NestedScrollView不适用于滚动行为。它适用于FrameLayout与CoordinatorLayout相同的文件。 –

+0

尝试将FrameLayout(它存在于coordinatorlayout中)的高度更改为'match_parent'。 – jitinsharma

+0

没有工作。我认为问题是NestedScrollView,每当我使用它所有的项目加载到onBindViewHolder。我试图设置NestedScrollView的高度不起作用。还有另一个布局可以用来代替NestedScrollView,并且具有滚动行为? –

相关问题