【发布时间】:2017-09-01 22:00:42
【问题描述】:
如何在不使用findFirstCompletelyVisibleItemPosition 的情况下检查回收站视图是否滚动到顶部(列表的开头)?
说明:我不能使用这个方法,因为RecyclerView上的项目很大,有时永远不会“完全可见”,所以该方法会返回-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
}
})
【问题讨论】:
-
我注意到你用
pull-to-refresh标记了,这到底有什么关系?如果我们知道更多细节,可能会有更好的解决方案。 -
背后的问题是 RecyclerView 在 SwipeRefreshLayout 内部。滑动有时会在错误的位置刷新。 IE。当列表不在顶部时。所以我只会在 RecyclerView 位于顶部时才允许刷新
-
啊,我明白了。这很奇怪,我在
RecyclerView中没有这个问题,但可能是那些较大的项目。 -
我发现,这个错误曾经发生在旧支持版本 (issuetracker.google.com/issues/37008297) 中,并且已修复。也许它正在发生,因为我使用的是 StaggeredGridLayout。 Idk,但谢谢!
标签: android android-recyclerview kotlin pull-to-refresh