With ListView
we have had a good native pattern to map some data from db to list:
DB
-> ContentProvider
-> CursorLoader
-> CursorAdapter
-> ListView
This approach was good in terms of data layer separation, performance and automatic data updates. But this pattern doesn't really fit new RecyclerView
. There are some approaches to mimic old behavior:
Using the recyclerview with a database
But there is a problem with using old style notifyDataSetChanged
with RecyclerView
. It can't use ItemAnimator features, it loses scrolling position, and it's just ineffective.
So, how we can benefit from finegraned change notifications while using DB wrapped in ContentProvider
? Cursor
is static, and to get new data from it we need to get new Cursor
. So, it seems that we will need an custom intermediate data layer, which will merge data from Cursors and expose the List
of entities to RecyclerView.Adapter
. Also, we will have to manually map ContentObserver
onChange()
events to RecyclerView notifications. This also means that we will have to get rid of CursorLoader. That is an incredible amount of work for such basic task.
Is there any better solution?