[英]Kotlin - Scroll Detection issue in Recyclerview
Using below code to check that whether RecyclerView reached to bottom or not..使用下面的代码来检查 RecyclerView 是否到达底部..
Means Checking that last item of the Recyclerview is visible or not..意味着检查 Recyclerview 的最后一项是否可见。
For that I have googled and added Scroll listener in Recyclerview.为此,我在 Recyclerview 中搜索并添加了 Scroll 侦听器。
Using below code:使用以下代码:
MyRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (!MyRecyclerView.canScrollVertically(1)) {
Toast.makeText(mContext, "Last", Toast.LENGTH_LONG).show();
}
}
})
Here, I am trying to check that Recyclerview reached to bottom or not.在这里,我试图检查 Recyclerview 是否到达底部。
Toast is not displaying when I scroll recyclerview to the bottom.当我将 recyclerview 滚动到底部时,Toast 没有显示。
But, Toast displayed suddenly when items in the recyclerview binds first time.但是,当回收器视图中的项目第一次绑定时,Toast 会突然显示。 It should toast only when user scroll up to the bottom of the Recyclerview.
仅当用户向上滚动到 Recyclerview 底部时才应敬酒。
What might be the issue ?可能是什么问题? Please guide.
请指导。 Thanks.
谢谢。
I think, using RecyclerView.LayoutManager
api will better suit your needs.我认为,使用
RecyclerView.LayoutManager
api 会更好地满足您的需求。 You can check LinearLayoutManager::findLastCompletelyVisibleItemPosition
or findLastVisibleItemPosition
method in your RecyclerView.OnScrollListener
您可以在
RecyclerView.OnScrollListener
检查LinearLayoutManager::findLastCompletelyVisibleItemPosition
或findLastVisibleItemPosition
方法
rvPlayers?.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy > 0) {
if (isLastVisable()) {
//code here
}
}
}
})
private fun isLastVisable(): Boolean {
val layoutManager = rvPlayers.layoutManager as LinearLayoutManager
val pos = layoutManager.findLastCompletelyVisibleItemPosition()
val numItems = adapter.itemCount
return (pos >= numItems - 1)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.