android - addOnScrollListener 的 RecyclerView onScrolled 方法只调用一次

标签 android android-recyclerview

背景:

我正在通过添加 RecyclerView 函数来使用 addOnScrollListener 实现分页逻辑。

面临的问题:

当 RecyclerView 适配器加载 dy 值为 0 时调用 onScrolled 方法一次。但是,当列表在滚动时到达其末尾时,不会触发 onScrolled 方法,而 onScrollStateChanged 被正确触发。

这是我的适配器:

HistoryAdapter(Context context, ArrayList<HistoryItem> items, RecyclerView recyclerView) {
        inflater = LayoutInflater.from(context);
        this.items = items;
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        this.context = context;
        this.recyclerView = recyclerView;

        final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Timber.w("onScrolled.. %s", dy);

                if (dy > 0) {
                    totalItemCount = linearLayoutManager.getItemCount();
                    lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                    Timber.e("totalItemCount: %s", totalItemCount);
                    Timber.e("lastVisibleItem: %s", lastVisibleItem);

                    if (!isLoading && totalItemCount <= (lastVisibleItem + 1)) {

                        Timber.i("End reached...");
                        if (onLoadMoreListener != null) {
                            //Log.i(TAG, "Episodes Adapter: onLoadMore calling...");
                            onLoadMoreListener.onLoadMore();
                        }
                        isLoading = true;
                    }
                }
            }

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }
        });
    }

回收站:
private void setupRecycler() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        historyAdapter = new HistoryAdapter(getActivity(), historyItems, recyclerView);
        recyclerView.setAdapter(historyAdapter);
        historyAdapter.setClickListener(this);
        historyAdapter.setOnLoadMoreListener(this);
        historyAdapter.setErrorClickListener(this);
    }

我的回收站 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:orientation="vertical">

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/medium_text_color_blue"
        android:padding="12dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="start"
            android:text="#"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4.5"
            android:gravity="start"
            android:text="CLASS"
            android:textAllCaps="true"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:text="Date"
            android:textAllCaps="true"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:text="time"
            android:textAllCaps="true"
            android:textColor="@android:color/white"
            android:textStyle="bold" />
    </LinearLayout>

    <FrameLayout
        android:layout_below="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/no_reflection_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:layout_gravity="center"
            android:text="No Reflection History found!" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/history_recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </FrameLayout>
</RelativeLayout>

我研究过的:

StackOverflow 上已经发布了许多这样的问题,但其中大多数都指向一个解决方案,即删除 ScrollView 的父 RecyclerView 阻止触发 onScrolled 方法,但这似乎不是我面临的问题。

RecyclerView.onScrolled() not getting called

RecyclerView onScrolled not called when use scrollToPosition

RecyclerView onScrolled not being fired at all

RecyclerView not calling onScrolled at the bottom

android RecyclerView onScrolled not call

https://github.com/passsy/ArrayAdapter/issues/4

最佳答案

另一种方法是检查回收器是否可以垂直滚动,如果不能,则表示它已到达底部。

尝试这个

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)

                if (!recyclerView.canScrollVertically(1)) {
                    // Load more
                }
            }
}

`

关于android - addOnScrollListener 的 RecyclerView onScrolled 方法只调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59694298/

相关文章:

android - 将 Xamarin.Forms.Color 转换为平台特定颜色

新版Dagger2中的android ObjectGraph

android - 使用计时器从 JSON 添加多个标记

android - 使 recyclerview 选择跟踪器的选择模式仅在长按时激活

android - 具有不同卡片和不同数据源的 Recyclerview

java - 如何在 RecyclerView 中检索值 我尝试了很多,但在 Android 中将数据推送到数据库后,它从 Firebase 生成的 ID 中检索空值

android - 如何知道用户是否在我的 Activity 之上?

java - 你能为viewpager2设置OnClickListener吗?

android - 页面固定在 recyclerview 中的特定帖子

android - 用户滑动时展开/折叠布局