检查 RecyclerView 是否滚动到顶部(findFirstCompletelyVisibleItemPosition 将不起作用)
Check if RecyclerView is scrolled on top (findFirstCompletelyVisibleItemPosition will not work)
如何在不使用 findFirstCompletelyVisibleItemPosition
?
说明:我不能使用这个方法,因为 RecyclerView
上的项目很大,有时永远不会 "completely visible",所以该方法将 return -1 (RecyclerView.NO_POSITION
)
我需要知道这一点才能仅在回收器视图到达顶部时触发操作。
val manager = timelineRecyclerView?.layoutManager as StaggeredGridLayoutManager
val visibles = manager.findFirstCompletelyVisibleItemPositions(null)
已解决:包括一些代码以提供帮助
timelineRecyclerView.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)
currentTimelinePosition += dy
Log.wtf("position", currentTimelinePosition.toString())
//if the timeline position is on its start, allow swipe and refresh
swipeLayout.isEnabled = currentTimelinePosition == 0
}
})
您可以创建一个 RecyclerView.OnScrollListener
,并跟踪它滚动了多少。
比如你只需要保留一个变量,根据垂直滚动量用onScroll更新即可。
class MyScrollListener() : RecyclerView.OnScrollListener() {
var currentScrollPosition = 0
override fun onScrolled(view: RecyclerView, dx: Int, dy: Int) {
currentScrollPosition += dy
if( currentScrollPosition == 0 ) {
// We're at the top
}
}
}
如何在不使用 findFirstCompletelyVisibleItemPosition
?
说明:我不能使用这个方法,因为 RecyclerView
上的项目很大,有时永远不会 "completely visible",所以该方法将 return -1 (RecyclerView.NO_POSITION
)
我需要知道这一点才能仅在回收器视图到达顶部时触发操作。
val manager = timelineRecyclerView?.layoutManager as StaggeredGridLayoutManager
val visibles = manager.findFirstCompletelyVisibleItemPositions(null)
已解决:包括一些代码以提供帮助
timelineRecyclerView.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)
currentTimelinePosition += dy
Log.wtf("position", currentTimelinePosition.toString())
//if the timeline position is on its start, allow swipe and refresh
swipeLayout.isEnabled = currentTimelinePosition == 0
}
})
您可以创建一个 RecyclerView.OnScrollListener
,并跟踪它滚动了多少。
比如你只需要保留一个变量,根据垂直滚动量用onScroll更新即可。
class MyScrollListener() : RecyclerView.OnScrollListener() {
var currentScrollPosition = 0
override fun onScrolled(view: RecyclerView, dx: Int, dy: Int) {
currentScrollPosition += dy
if( currentScrollPosition == 0 ) {
// We're at the top
}
}
}