这是我的功能,用于检测结尾的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
的GridLayoutManager
一起使用时,事情变得很奇怪。totalItemCount-1
。 最后一件事:我在
onCreate
中调用此函数,并尝试了具有RecyclerView
的linear 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/