Get visible items in RecyclerView

前端 未结 9 1969
礼貌的吻别
礼貌的吻别 2020-11-22 07:17

I need to know which elements are currently displayed in my RecyclerView. There is no equivalent to the OnScrollListener.onScroll(...) method on ListViews. I tried to work w

9条回答
  •  粉色の甜心
    2020-11-22 08:12

    You can find the first and last visible children of the recycle view and check if the view you're looking for is in the range:

    var visibleChild: View = rv.getChildAt(0)
    val firstChild: Int = rv.getChildAdapterPosition(visibleChild)
    visibleChild = rv.getChildAt(rv.childCount - 1)
    val lastChild: Int = rv.getChildAdapterPosition(visibleChild)
    println("first visible child is: $firstChild")
    println("last visible child is: $lastChild")
    

提交回复
热议问题