MaaAn13
MaaAn13

Reputation: 268

RecyclerView calls onBindViewHolder() on all items if RecyclerView is inside NestedScrollView

View structure:

<CoordinatorLayout 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

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

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

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:visibility="gone"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

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

            <android.support.v7.widget.RecyclerView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                android:orientation="vertical"
                android:visibility="visible"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_goneMarginTop="0dp" />

            <android.support.v7.widget.RecyclerView  
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:clipToPadding="false"
                android:nestedScrollingEnabled="false"
                android:visibility="gone"
                app:layout_goneMarginTop="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent" />

        </ConstraintLayout>
    </NestedScrollView>
</CoordinatorLayout>

What actually happens is, that second RecyclerView in the list acts weird. Usueally onBindViewHolder method gets called on visibile items. Well, in my case, it calls on all items - thus, I've problems of setting Listeners on specific items (when they're binded). I must use NestedScrollView. Is there a solution for this nonsense?

Upvotes: 1

Views: 1148

Answers (1)

MaaAn13
MaaAn13

Reputation: 268

Took me some time to come up with a workaround. I set my mRecyclerViews nestedScrollingEnabled to false and then set a listener for my mNestedScrollView.

Listner:

mNestedScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
           @Override
           public void onScrollChanged()
           {
                    View v = (View)mNestedScrollView.getChildAt(mNestedScrollView.getChildCount() - 1);

                    int diff = (v.getBottom() - (mNestedScrollView.getHeight() + mNestedScrollView.getScrollY()));

                    if (diff == 0) {
                       // pagination
                    }
           }
  });

Upvotes: 1

Related Questions