【问题标题】:RecyclerView onScrolled not being fired at allRecyclerView onScrolled 根本没有被解雇
【发布时间】:2018-10-07 20:38:24
【问题描述】:

我打算创建一个分页滚动到底部的 RecyclerView。但是 onScrolled 回调根本没有被触发:

    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);
    }

我也尝试从 NestedScrollView 中删除 RecyclerView,但它仍然不起作用。

这是我的 XML 文件:

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>

最初,它位于NestedScrollView 下,但由于提到如果RecyclerViewNestedScrollViewScrollView 内,则不会调用onScroll,因此我删除了NestedScrollView

【问题讨论】:

  • 你能用 layout.xml 更新你的问题吗?
  • @Sagar 我已经用布局更新了我的帖子。

标签: java android android-recyclerview gridlayoutmanager onscrolllistener


【解决方案1】:

我重新启动了我的 Android Studio,它现在一直在工作。但修复可能是删除 NestedScrollView 中的 RecyclerView。

【讨论】:

  • 如果你使用 NestedScrollView,那么你将滚动监听器添加到 NestedScrollView 而不是 RecyclerView
  • 离题建议,这是我的情况。 NestedScrollView 将阻止按预期触发滚动事件(有时总是有时根本没有)。
  • 好技巧!解决了我的问题
【解决方案2】:

您已禁用 RecyclerView 的滚动。因此,您将不会收到其滚动的任何事件。您需要改为添加 recyclerView 父级的滚动事件,该事件应该是 NestedScrollView 而不是 ScrollView。

【讨论】:

  • 我在代码的哪一部分禁用了 RecyclerView 的滚动?
  • mBooksRecyclerView.setNestedScrollingEnabled(false);
  • 我不认为这会禁用滚动,但我也注释掉了该代码,它仍然不起作用。
  • 您可以阅读文档here。而recyclerView在取入NestedScrollView时,存在2个滚动条。所以,为了停止 RecyclerView 的滚动,使用了这个方法。即使在评论该代码后您的情况下也没有收到事件,那么您必须特别滚动包含 recyclerView 的部分才能接收事件。
【解决方案3】:

试试这个

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

现在用这个方法实现分页

 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
                    }
                }
            }
        });

【讨论】:

  • 我的问题是,onScrolled 没有被调用。
  • 删除此行并尝试 mBooksRecyclerView.setNestedScrollingEnabled(false);
猜你喜欢
  • 2020-03-28
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
相关资源
最近更新 更多