RecyclerView onScrolled not being fired at all
Asked Answered
S

3

3

I am planning to create a paginated scroll-to-bottom RecyclerView. But the onScrolled callback isn't being fired at all:

    mBooksRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            Log.i("Dale", "scrolled");
        }
    });

    mBooksRecyclerView.setNestedScrollingEnabled(false);

    if (Utils.hasContent(books)) {
        mBooksRecyclerView.setVisibility(View.VISIBLE);
        BookCardViewAdapter adapter = new BookCardViewAdapter(this, books);

        final GridLayoutManager gridLayoutManager = new GridLayoutManager(BooksActivity.this, 3);
        mBooksRecyclerView.setLayoutManager(gridLayoutManager);
        mBooksRecyclerView.setAdapter(adapter);

        emptyView.setVisibility(View.GONE);
    } else {
        emptyView.setVisibility(View.VISIBLE);
        mBooksRecyclerView.setVisibility(View.GONE);
    }

I also tried removing the RecyclerView from a NestedScrollView and it still doesn't work.

Here is my XML files:

books_content.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="@dimen/books_category_height"
        android:background="@color/gfs_blue">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/categories_tab"
            android:layout_width="match_parent"
            android:layout_height="@dimen/books_category_height">

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/sub_categories_tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header"
        android:layout_marginTop="20dp">

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/books_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@id/sub_categories_tab"
        android:scrollbars="vertical" />
</RelativeLayout>

activity_books.gfs:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:fitsSystemWindows="true"
        tools:context=".activities.GFSBooksActivity">

        <include
            android:id="@+id/appbarlayout"
            layout="@layout/appbarlayout"/>

        <RelativeLayout
            android:id="@+id/empty_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:layout_below="@id/appbarlayout"
            android:background="@color/white"
            android:visibility="gone">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="No content found"
                android:textColor="@color/gray"
                android:textSize="20dp" />
        </RelativeLayout>

        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/books_content"
            android:layout_below="@+id/appbarlayout"/>
        <!--<android.support.v4.widget.NestedScrollView-->

            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="match_parent"-->
            <!--app:layout_behavior="@string/appbar_scrolling_view_behavior">-->


        <!--</android.support.v4.widget.NestedScrollView>-->

    </RelativeLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

Initially, it was under a NestedScrollView but since it was mentioned that onScroll wouldn't be called if the RecyclerView is inside a NestedScrollView or a ScrollView, I removed the NestedScrollView.

Subaquatic answered 27/4, 2018 at 3:52 Comment(2)
Can you updated your question with layout.xml?Postconsonantal
@Postconsonantal I have updated my post with the layouts.Subaquatic
S
9

I restarted my Android Studio and it is now working all along. But the fix probably was removing the RecyclerView inside the NestedScrollView.

Subaquatic answered 27/4, 2018 at 6:25 Comment(3)
If you use NestedScrollView, then you add scroll listener to NestedScrollView and not on RecyclerViewPostconsonantal
Off-topic suggestion which was my case. NestedScrollView will prevent from firing the scroll events as supposed to (sometimes all the time sometimes none at all).Publican
Good hack ! Solved my probelmDaveta
H
2

You have disabled RecyclerView's scrolling. Hence, you will not receive any events of its scroll. You need to instead add the scroll event of parent of recyclerView which should be NestedScrollView not ScrollView.

Heads answered 27/4, 2018 at 3:57 Comment(4)
In which part of the code have I disabled RecyclerView's scrolling?Subaquatic
mBooksRecyclerView.setNestedScrollingEnabled(false);Heads
I don't think that will disable the scrolling, but I also commented out that code and it still doesn't work.Subaquatic
You may read the docs here. And recyclerView when taken in a NestedScrollView, there exists 2 scrolls. So, in order to stop scrolling of RecyclerView, this method is used. And left case of yours not receiving the event even after commenting that code, then you have to particularly scroll the part containing recyclerView to receive event.Heads
G
0

Try this

int pastVisibleItem, visibleItemCount, totalItemCount, currentPage = 1, mPage = 1;
private boolean loading = true;

now use this method to implement pagination

 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //Log.e("checkkk", "cheeckk");

                visibleItemCount = recyclerView.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
                if (loading) {
                    if ((visibleItemCount + pastVisibleItem) >= totalItemCount) {
                        loading = false;
                        currentPage++;
                        // do your stuff here
                    }
                }
            }
        });
Griz answered 27/4, 2018 at 5:13 Comment(2)
My problem is, onScrolled is not being called.Subaquatic
remove this line and try mBooksRecyclerView.setNestedScrollingEnabled(false);Griz

© 2022 - 2024 — McMap. All rights reserved.