Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recyclerview view is not working with setNestedScrollingEnabled and its never recycle any views also

I am facing two problems:

  1. The scroll listener won't work
  2. The RecyclerView never recycles any views when it's attached to a NestedScrollView. It acts like a linear layout inside the ScrollView. It uses a lot of memory and creates lags.

I am attaching a youtube player fragment on top of the recycler view since I can't put a fragment inside the recycler view. In my code you can see there is a frame layout.

My layout looks like this:

    <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nestedScroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <FrameLayout               
                android:layout_width="match_parent"
                android:layout_height="240dp"
                android:layout_alignParentTop="true"/>





        </LinearLayout>


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/youtube_layout"
            android:visibility="visible"/>




    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

I want to use scroll listener to load more items so I have tried the following:

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    Log.i(TAG, "onScrolled: ");
    // bail out if scrolling upward or already loading data
    if (dy < 0 || dataLoading.isDataLoading()) return;

    final int visibleItemCount = recyclerView.getChildCount();
    final int totalItemCount = layoutManager.getItemCount();
    final int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();

    if ((totalItemCount - visibleItemCount) <= (firstVisibleItem )) {
        onLoadMore();
    }


}

But layoutManager.findFirstVisibleItemPosition()==0 so it's not working, and more over onScrolled never called twice since I set recyclerview.setNestedScrollingEnabled(false)

so I have tried in onBindView like this

   public void onBindViewHolder(RecyclerView.ViewHolder customViewHolder, int i) {
    Log.w("d","inside bind view");

    if(i>=getItemCount()-1  &&   !datamanager.isDataLoading()){
        datamanager.loadmoreData();
    }

but the recylerview binds the all the view at once time before I even start scrolling, so this method also does not work.

like image 356
Asthme Avatar asked Aug 03 '17 07:08

Asthme


1 Answers

The recycler view never recycle any views when its attached to nested scroll view,its acts as set on linear layout inside scroll view.its create a huge memory and lags the screen.

Exactly. You can't put a RecyclerView inside another scrolling view.

It will not work, because the wrapping view needs to know the total height of the RecyclerView and the RecyclerView can only know its total height by measuring and layouting all of its items.

How to fix this?

Don't put a RecyclerView inside another scrolling view.

If you need a header or footer you will have to add it to the RecyclerView and let the RecyclerView take care of displaying it. Get rid of your ScrollView and move your header inside of the RecyclerView.

Technically it is possible to also load fragments inside a RecyclerView, but I have to admit getting this to work properly is a bit tricky.

There are also a lot of libraries that facilitate the process, my personal favorite being Epoxy made by AirBnb, but there's also Groupie, and a lot of others.

like image 188
David Medenjak Avatar answered Sep 20 '22 15:09

David Medenjak