Inserting RecyclerView items at zero position - always stay scrolled to top

前端 未结 5 613
不知归路
不知归路 2021-01-01 12:32

I have a pretty standard RecyclerView with a vertical LinearLayoutManager. I keep inserting new items at the top and I\'m calling notifyItemI

5条回答
  •  生来不讨喜
    2021-01-01 13:05

    This worked for me:

    val atTop = !recycler.canScrollVertically(-1)
    
    adapter.addToFront(item)
    adapter.notifyItemInserted(0)
    
    if (atTop) {
        recycler.scrollToPosition(0)
    }
    

提交回复
热议问题