How to use LiveData with RecycleView correctly

I have a project that downloads a list from a server. This data will eventually be stored in the database, but for now stored in memory in MutableLiveData. The RecyclerView adapter views the data and displays it. While everything works as expected, using FAB, the user can post a new entry that will go to the top of the list, if successful, I will get 200 and here is the main part where I get lost ...

When I want to add one item to the list stored in LiveData, the observer is not aware of the delta. I am currently calling in RecyclerView.Adapter.notifyDataSetChanged(), although the call would be ideal in my case notifyItemInserted(0), or in other cases I may see various other notifications. What is the best way to do this? The life-cycle architecture library seems to be very well thought out, I guess I missed something simple. I can not imagine to manually make the difference between lists?

+4
source share

Source: https://habr.com/ru/post/1686961/


All Articles