RecyclerView onScrolled根本没有被解雇 [英] RecyclerView onScrolled not being fired at all

查看:91
本文介绍了RecyclerView onScrolled根本没有被解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正计划创建一个分页滚动到底部的RecyclerView.但是onScrolled回调根本没有被触发:

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

我还尝试了从NestedScrollView中删除RecyclerView,但它仍然无法正常工作.

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

这是我的XML文件:

books_content.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:

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.

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.

推荐答案

我重新启动了Android Studio,现在它一直可以正常工作.但是解决方法可能是删除NestedScrollView中的RecyclerView.

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

这篇关于RecyclerView onScrolled根本没有被解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆