如何判断Item在RecyclerView中是否滑动到顶部或底部
方法1: 比较推荐,和你设置的layoutManager无关
_recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (!_recyclerView.canScrollVertically(1)) {
//滑动到底部
}
if (!_recyclerView.canScrollVertically(-1)) {
//滑动到顶部
}
}
})
_recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val layoutManager = recyclerView.layoutManager as LinearLayoutManager
val firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition()
if (firstCompletelyVisibleItemPosition == 0) {
//滑动到顶部
}
val lastCompletelyVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition()
if (lastCompletelyVisibleItemPosition == layoutManager.getItemCount() - 1) {
//滑动到底部
}
}
})
|