5

I am using multiple View-holder inside recycler which placed inside nested scroll view,There is a change in Natural behaviour of onBindViewHolder() Recycler view because of Nested Scroll,getItemViewType() all items are called inside onBindViewHolder() when initiate recycler adapter,For Example I have 20 Items means in normal scenario only three items called when initiate,but in case of nested scroll view all 20 views create on first load.

Xml File

<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="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/light_gray_vd">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="@color/primaryColor"
            app:expandedTitleMarginEnd="16dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax">

                <include
                    android:id="@+id/inc_gallery"
                    layout="@layout/proj_galery_new"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />

            </FrameLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar1"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="top"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                <include
                    android:id="@+id/toolbar_header_view"
                    layout="@layout/header_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone" />
            </android.support.v7.widget.Toolbar>

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

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/vde_mv_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            </android.support.v7.widget.RecyclerView>



        </FrameLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
appukrb
  • 1,507
  • 4
  • 24
  • 53
  • Recyclerview inside Scrollview is never a good idea. I'm assuming you gave Recyclerview fixed height. I think that where the problem is. – Hein Feb 08 '16 at 06:52
  • @Hein I used only wrap content,but nested scroll view allot full space for recycler – appukrb Feb 08 '16 at 06:57
  • Can you put up said layout file ? – Hein Feb 08 '16 at 06:59
  • I see you are using CoordinatorLayout with CollapsingToolbarLayout. You want to collapse toolbar by scrolling recyclerview ? – Hein Feb 08 '16 at 07:22
  • @Hein yes I need to collapse with recycler view – appukrb Feb 08 '16 at 08:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102879/discussion-between-hein-and-appukrb). – Hein Feb 08 '16 at 09:07

3 Answers3

2

remove NestedScrollView and frameLayout, and set app:layout_behavior="@string/appbar_scrolling_view_behavior" for recycleView like this:

<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="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/light_gray_vd">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="@color/primaryColor"
            app:expandedTitleMarginEnd="16dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax">

                <include
                    android:id="@+id/inc_gallery"
                    layout="@layout/proj_galery_new"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />

            </FrameLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar1"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="top"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                <include
                    android:id="@+id/toolbar_header_view"
                    layout="@layout/header_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone" />
            </android.support.v7.widget.Toolbar>

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

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

    <android.support.v7.widget.RecyclerView
                android:id="@+id/view_recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:scrollbars="vertical">

            </android.support.v7.widget.RecyclerView>

</android.support.design.widget.CoordinatorLayout>
javad
  • 833
  • 14
  • 37
0

The issue is both Recyclerview and NestedScrollview are scrollable. By removing one, you should be able to achieve the effect you wanted.

So, try using only Recyclerview without putting it inside NestedScrollView. Like this :

<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="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/light_gray_vd"
    >
  <android.support.design.widget.AppBarLayout
      android:id="@+id/app_bar_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
      >

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentScrim="@color/primaryColor"
        app:expandedTitleMarginEnd="16dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        >

      <FrameLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:fitsSystemWindows="true"
          app:layout_collapseMode="parallax"
          >

        <include
            android:id="@+id/inc_gallery"
            layout="@layout/proj_galery_new"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />

      </FrameLayout>

      <View
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          />

      <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar1"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:layout_gravity="top"
          app:layout_collapseMode="pin"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
          >

        <include
            android:id="@+id/toolbar_header_view"
            layout="@layout/header_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            />
      </android.support.v7.widget.Toolbar>

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

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

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

You can also reference this example or read this article from Code path about how to handle scroll with CoordinatorLayout

CheeseSquare from Chris Bane also has a good CoordinatorLayout example

Hein
  • 2,675
  • 22
  • 32
0

I also had same issue after Android Support Library 23.2.0 update.

Set RecyclerView's android:layout_height to match_parent.

And disable new automeasure feature (read more):

LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), 
    LinearLayoutManager.VERTICAL, false);
layoutManager.setAutoMeasureEnabled(false);
recyclerView.setLayoutManager(layoutManager);
mixel
  • 25,177
  • 13
  • 126
  • 165