簡體   English   中英

Recyclerview addOnScrollListener問題

[英]Recyclerview addOnScrollListener issue

這是我檢測到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
            }
        }
    })
}

我以前在具有線性LayoutManger RecyclerView上使用它,並且所有功能都很棒。

但是,當我將其與具有GridLayoutManager RecyclerView一起使用時,事情變得很奇怪。

  1. 它僅實現一次,並且在我滾動時不起作用。
  2. 最后可見項目位置返回totalItemCount-1

最后一件事:我在onCreate調用此函數,並使用具有linear layout manager RecyclerView了嘗試,一切都很棒

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

回答比問題更奇怪

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM