【问题标题】:Recyclerview addOnScrollListener issueRecyclerview addOnScrollListener 问题
【发布时间】:2019-03-11 13:22:04
【问题描述】:

这是我的函数,用于检测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,一切都很棒。

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

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

最后一件事:我在onCreate 中调用了这个函数,我用RecyclerView 尝试了它,它有linear 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

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

【问题讨论】:

  • @pskink 我不明白...

标签: android android-recyclerview kotlin pagination endlessscroll


【解决方案1】:

我的 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 就可以了

回答比问题更奇怪

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 2016-01-09
相关资源
最近更新 更多