Recyclerview addOnScrollListener问题

时间:2018-10-06 04:29:44

标签: android android-recyclerview kotlin pagination endlessscroll

这是我检测到结尾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,而且一切都很棒。

但是当我将它与具有RecyclerView的{​​{1}}一起使用时,事情变得很奇怪。

  1. 它仅实现一次,并且在滚动时不起作用。
  2. 最后一个可见的项目位置返回GridLayoutManager

最后一件事:我在totalItemCount-1中调用了此函数,并使用具有onCreate的{​​{1}}进行了尝试,一切都很好

这是我的logcat,

RecyclerView

它只调用一次,滚动时什么也没发生。

1 个答案:

答案 0 :(得分:0)

我的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,并且可以使用

答案比问题更奇怪