RecyclerView inside NestedScrollView onBindViewHolder calling for all getItemCount size
Asked Answered
S

4

12

When I put RecyclerView inside NestedScrollView then onBindViewHolder is calling for all row like say I have list which has size of 30 then onBindViewHolder is called for all 30 rows at one time even without scrolling

 RecyclerView list;
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        list.setLayoutManager(layoutManager);
        layoutManager.setAutoMeasureEnabled(true);
        list.setNestedScrollingEnabled(false);
        list.addItemDecoration(new VerticalSpaceItemDecoration(5));
        list.setAdapter(adapter);

my xml is

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:scrollbars="none"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/grey">
    <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_views"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/info"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:textAlignment="center"

            android:visibility="visible"
           />

but if I remove NestedScrollView it's working properly.

Somite answered 19/5, 2016 at 11:38 Comment(3)
Did you find any solution for this problem,This is definitely related to RecylerView inside NestedScrollViewOgham
any solution for this problem? Facing the same issue.Grekin
https://mcmap.net/q/949914/-recyclerview-inside-nestedscrollview-onbindviewholder-calling-for-all-getitemcount-size answer also didn't work for me.Grekin
F
2

I'm going to assume that since your are using appbar_scrolling_view_behavior you are trying to do something with AppBarLayout.

If so, you can use RecyclerView as a direct child of CoordinatorLayout and have support for AppBarLayout scrolling without nesting RecyclerView inside of NestedScrollView.

Try this: RecyclerView inside CoordinatorLayout (with AppBarLayout and CollapsingToolbarLayout):

<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:background="#55FF00FF"
                app:layout_collapseMode="none"/>

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

And in your Activity or CustomView:

RecyclerView list;
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
list.setLayoutManager(layoutManager);
list.addItemDecoration(new VerticalSpaceItemDecoration(5));
list.setAdapter(adapter);
Feingold answered 1/6, 2016 at 3:8 Comment(1)
Hi, I have the same issue. In my case, I have a FragmentContainerView which loads the fragment that contains a recyclerview. How can i tackle this issue?Guilbert
C
1

But you set android:layout_height for NestedScrollView to wrap_content - here, it's zero by default (because there no content for him at the moment of the declaration). Next, for RecyclerView you set android:layout_height to match_parent - which is at the moment is 0. Thus, all your items have 0 height.

Thus, you have such situation. Solution: use solution above from @dkarmazi https://mcmap.net/q/949914/-recyclerview-inside-nestedscrollview-onbindviewholder-calling-for-all-getitemcount-size or try to change parameter android:layout_height values.

Cato answered 1/6, 2016 at 3:16 Comment(0)
C
1

It's right.Because you are using a ScrollView.ScrollView is not recyclable like RecyclerView or ListView.It will show all view contains these out of screen in one time.You should use a other layout instead.

Centuple answered 8/6, 2016 at 1:14 Comment(0)
T
0

I faced the same issue. After a bit of research fount the solution.

You need to make sure your recyclerview height is fixed by setting it to MATCH_PARENT. Or if its in a contraint layout then set height to 0dp and set the required height constrains. Then set recyclerview.setHasFixedSize to true.

The onBindViewHolder will start getting called after this.

Taken answered 20/11, 2022 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.