如何判断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)) {
//滑动到顶部
}
}
})
方法2
_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) {
//滑动到底部
}
}
})
附: UtilKView
/**
* 是否滑动到底部
* @param recyclerView RecyclerView
* @return Boolean
*/
fun isScroll2Top(recyclerView: RecyclerView): Boolean = !recyclerView.canScrollVertically(-1)
/**
* 是否滑动到底部
* @param recyclerView RecyclerView
* @return Boolean
*/
fun isScroll2End(recyclerView: RecyclerView): Boolean = !recyclerView.canScrollVertically(1)
/**
* 滑动到顶部2
* @param recyclerView RecyclerView
* @return Boolean
*/
fun isScroll2Top2(recyclerView: RecyclerView): Boolean {
if (recyclerView.layoutManager is LinearLayoutManager) {
val linearLayoutManager = recyclerView.layoutManager as LinearLayoutManager
val firstItemPos = linearLayoutManager.findFirstVisibleItemPosition()
val lastItemPos = linearLayoutManager.findLastVisibleItemPosition()
val itemCount = linearLayoutManager.itemCount
val lastChild: View = recyclerView.getChildAt(lastItemPos - firstItemPos)
if (lastItemPos == itemCount - 1 && lastChild.bottom <= recyclerView.measuredHeight) {
return true
}
} else if (recyclerView.layoutManager is StaggeredGridLayoutManager) {
val layoutManager = recyclerView.layoutManager as StaggeredGridLayoutManager
val firstVisibleItems =
(recyclerView.layoutManager as StaggeredGridLayoutManager).findFirstVisibleItemPositions(null)
// 真实Position就是position - firstVisibleItems[0]
val itemCount = layoutManager.itemCount
val lastPositions = IntArray(layoutManager.spanCount)
layoutManager.findLastVisibleItemPositions(lastPositions)
val lastPosition: Int = lastPositions.maxOf { it }
val lastChild: View? = recyclerView.getChildAt(lastPosition - firstVisibleItems[0])
if (lastPosition == itemCount - 1 && lastChild != null && lastChild.bottom <= recyclerView.getMeasuredHeight()) {
return true
}
}
return false
}
/**
* 滑动到底部2
* @param recyclerView RecyclerView
* @return Boolean
*/
fun isScroll2End2(recyclerView: RecyclerView): Boolean {
if (recyclerView.layoutManager is LinearLayoutManager) {
val linearLayoutManager = recyclerView.layoutManager as LinearLayoutManager
val firstItem = linearLayoutManager.findFirstVisibleItemPosition()
return recyclerView.getChildAt(0).y == 0f && firstItem == 0
} else if (recyclerView.layoutManager is StaggeredGridLayoutManager) {
val aa = (recyclerView.layoutManager as StaggeredGridLayoutManager).findFirstVisibleItemPositions(null)
return recyclerView.getChildAt(0).y == 0f && aa[0] == 0
}
return false
}
/**
* 是否滑动到边缘
* @param recyclerView RecyclerView
* @return Boolean
*/
fun isScroll2VerticalEdge(recyclerView: RecyclerView): Boolean =
isScroll2End(recyclerView) || isScroll2Top(recyclerView)
/**
* 是否滑动到边缘2
* @param recyclerView RecyclerView
* @return Boolean
*/
fun isScroll2VerticalEdge2(recyclerView: RecyclerView): Boolean =
isScroll2End2(recyclerView) || isScroll2Top2(recyclerView)
/**
* 是否向上滚动
* @param dy Int
* @return Boolean
*/
fun isScrollUp(dy: Int): Boolean = dy < 0
/**
* 是否向下滚动
* @param dx Int
* @return Boolean
*/
fun isScrollDown(dx: Int): Boolean = dx > 0