相关疑难解决方法(0)

NestedScrollView中的RecyclerView未提供正确的可见项位置

我有一个RecyclerView内部NestedScrollView,我试图在滚动事件使用期间获取最后一个可见列表项的位置.以下代码允许我可靠地检测滚动事件:

val recyclerView = nestedScrollView.getChildAt(0) as RecyclerView

nestedScrollView.setOnScrollChangeListener { v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int ->
    val layoutManager = recyclerView?.layoutManager as? LinearLayoutManager
    val lastVisiblePosition = layoutManager?.findLastVisibleItemPosition()
}
Run Code Online (Sandbox Code Playgroud)

然而问题是,这里lastVisiblePosition总是最终成为列表中的最后一项,无论这是否实际上是在scree上.我哪里可能出错?

android kotlin android-recyclerview android-nestedscrollview

2
推荐指数
1
解决办法
1332
查看次数