android - Recyclerview addOnScrollListener问题

标签 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
            }
        }
    })
}

我以前在具有线性RecyclerViewLayoutManger上使用过它,并且所有功能都很棒。

但是当我将它与具有RecyclerViewGridLayoutManager一起使用时,事情变得很奇怪。
  • 它仅实现一次,并且在我滚动时不起作用。
  • 最后可见的项目位置返回totalItemCount-1

  • 最后一件事:我在onCreate中调用此函数,并尝试了具有RecyclerViewlinear layout manager,一切都很好

    这是我的logcat,
    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
    

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

    最佳答案

    我的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,它可以工作

    回答比问题更奇怪

    关于android - Recyclerview addOnScrollListener问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52675709/

    相关文章:

    java - invalidate() 不会导致调用 onDraw()

    java - Java 中约束布局宽度和高度作为根父级

    android - StaggeredGridLayout 中 imageview 的自动缩放高度

    java - 无法在抽屉导航 fragment 中包含 RecyclerView

    android - 在没有 AppCompat 的情况下为 Android 5 编程

    android - 附加到底部的 AdView 不允许我在 Recyclerview 中看到最后一项

    android - 如何使用 Kotlin 延迟加载协程刷新 ViewModel 数据?

    java - 有没有办法在 Android Studio 中将代码从 Java 转换为 Kotlin,而不使用复制/粘贴方法?

    android - 在普通函数中调用挂起函数

    java - Android AsyncTask/doInBackground 模式