首页 > 解决方案 > Recyclerview addOnScrollListener 问题

问题描述

这是我的检测结束的函数,RecyclerView稍后将在分页中使用。

private fun setOnScrollListener(categoryRecyclerView: RecyclerView, categoryPresenter: EntertainmentPresenter){
    categoryRecyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener(){

        override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            val totalItemCount:Int = categoryLayoutManager.itemCount
            val firstVisibleItemPosition:Int = categoryLayoutManager.findFirstVisibleItemPosition()
            val visibleItemCount:Int
            val lastVisibleItemPosition: Int = categoryLayoutManager.findLastVisibleItemPosition()

            visibleItemCount = lastVisibleItemPosition - firstVisibleItemPosition

            Log.e("q", lastVisibleItemPosition.toString())
            Log.e("q", dy.toString())

            if (loading) {
                if (totalItemCount > previousTotal) {
                    loading = false
                    previousTotal = totalItemCount
                }
            }

            if (!loading && totalItemCount - visibleItemCount  <= firstVisibleItemPosition) {
                 Log.e("q", lastVisibleItemPosition.toString())
                loading = true
            }
        }
    })
}

我以前用过它,它RecyclerView是线性LayoutManger的,每件事都很棒。

但是当我将它与RecyclerViewthat has一起使用时GridLayoutManager,事情变得很奇怪。

  1. 它只实现了一次,并且在我滚动时不起作用。
  2. 最后可见项目位置返回totalItemCount-1

最后一件事:我调用了这个函数onCreate,我尝试了它RecyclerViewlinear layout manager一切都很棒

这是我的日志猫,

2018-10-06 06:10:15.919 11033-11033/com.example.karem.moviesdatabase E/q: 0  
2018-10-06 06:10:15.919 11033-11033/com.example.karem.moviesdatabase E/q: 18

它只调用一次,当我滚动时没有任何反应。

标签: androidandroid-recyclerviewkotlinpaginationendlessscroll

解决方案


我的 RecyclerView 高度设置为 match_parent

但是在我将高度更改为 500dp 后一切正常

我不知道为什么,但无论如何这是我的旧布局

<android.support.constraint.ConstraintLayout
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/layoutBackground"
tools:context=".fragments.AllItemsFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/categoryRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:listitem="@layout/entertainment_recycler_view_item" />

<ProgressBar
    android:id="@+id/loadingCategoryList"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="@+id/categoryRecyclerView"
    app:layout_constraintEnd_toEndOf="@+id/categoryRecyclerView"
    app:layout_constraintStart_toStartOf="@+id/categoryRecyclerView"
    app:layout_constraintTop_toTopOf="@+id/categoryRecyclerView" />

</android.support.constraint.ConstraintLayout>

我只将 recyclerview 高度更改为 500dp 并且它可以工作

回答比问题更奇怪


推荐阅读