android - Recyclerview addOnScrollListener 问题
问题描述
这是我的检测结束的函数,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
that has一起使用时GridLayoutManager
,事情变得很奇怪。
- 它只实现了一次,并且在我滚动时不起作用。
- 最后可见项目位置返回
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
它只调用一次,当我滚动时没有任何反应。
解决方案
我的 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 并且它可以工作
回答比问题更奇怪
推荐阅读
- kentico - Kentico 编辑器/所见即所得的问题
- pointers - 当鼠标仅在一个方向拖动以及鼠标释放捕捉到网格时如何平滑移动游戏对象(Unity)
- optimization - 优化谷歌脚本 - 访问 25 个电子表格
- javascript - Javascript动态onClick
- r - 在 R 中登录后下载/抓取/访问在线 PDF
- javascript - DropdDownlist 不显示所选值
- sql - 修剪将用于不同长度字符串的子字符串
- azure-ad-b2c - 如何在组合 signsignup 中预填充电子邮件地址
- c# - 具有百分比进度的 WebClient 下载文件
- spring - Mongodb 更新最新记录而不是创建新记录