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

Viewed 6017

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.

3 Answers
Related