I have Fragment
with CoordinatorLayout
and FrameLayout
to popular with other Fragments. One of these Fragments contains RecyclerView
.
I would like to know how to make RecyclerView
work with CoordinatorLayout
and each one is in a file. I tried to put NestedScrollView
as the parent of Fragment
that has RecyclerView
, but when I do this onBindViewHolder
of adapter RecyclerView
is called for all elements and then I can not do paged search to fill the RecyclerView
.
Main Fragment code containing 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>
Fragment code that has the 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>
That way scroll even works, however, onBindViewHolder
is called for all items in the list, even for those that are not being displayed. If I put a LinearLayout
instead of NestedScrollView
, onBindViewHolder
works the right way but the behavior of scroll @string/appbar_scrolling_view_behavior
with CoordinatorLayout
does not work.