Recycler view not scrolling to the top after adding new item at the top, as changes to the list adapter has not yet occurred

后端 未结 5 1475
无人及你
无人及你 2021-02-07 06:37

I am getting the new list with new item at the start in my live data and then using its data to update the adapter

viewModel.myLiveData.observe { this, Observer          


        
5条回答
  •  长情又很酷
    2021-02-07 06:53

    Use this way

    viewModel.myLiveData.observe { this, Observer { myList -> 
        adapter.submitList(myList) // Assuming you are notifying adapter by notifydatasetchanged()
        recyclerView.post { recyclerView.scrollToPosition(0) }
    }
    

    Here post is giving UI Thread some time to populate Recycler with new data and then call scrollToPosition.

提交回复
热议问题