Android how can I get current positon on recyclerview that user scrolled to item

后端 未结 5 1475
清歌不尽
清歌不尽 2020-12-25 11:21

In my RecyclerView I have some items that user can scroll and see that. Now I want to save this position and scroll that after come back. This below code return

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-25 12:02

    This is the extension function in Kotlin:

    private fun RecyclerView.getCurrentPosition() : Int {
        return (this.layoutManager as LinearLayoutManager?)!!.findFirstVisibleItemPosition()
    }
    

    You can use it just invoking this function on your RecyclerView:

    val position = yourRecyclerView.getCurrentPosition()
    

提交回复
热议问题