Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView inside NestedScrollView onBindViewHolder calling for all getItemCount size

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.

like image 351
andro Avatar asked May 19 '16 11:05

andro


People also ask

How to use recyclerview inside nestedscrollview in Android?

This example demonstrates how do I use RecyclerView inside NestedScrollView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Add the following dependency in the build.gradle (Module: app)

How can I extend the size of a recyclerview?

In such situations you have either the option to give the RecyclerView a fixed size and the user can scroll the items within that box or you wrap it in a NestedScrollView. This component allows you to not only scroll through views that extend the size of the device screen but also use complex layouts inside like the RecyclerView.

How to get Total item count of a set in recyclerview?

RecyclerView.Adapter already has function getItemCount for getting total item count. Show activity on this post. Get total item count from adapter of set in recyclerView. recyclerView & adapter can't be null, otherwise you can find total items.

What happens if the nestedscrollview isn’t scrollable?

So if the NestedScrollView isn’t currently scrollable (for example when it has only little content) or if it’s already scrolled to the end in the direction the user wants to scroll, nothing happens. That’s a horrible user experience! Here it looks broken to the user:


1 Answers

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);
like image 125
dkarmazi Avatar answered Nov 15 '22 17:11

dkarmazi