Share
Embed
How to notify Dataset changed for RecyclerView
Think about a better way to notify dataset changed for RecyclerView.
Ryota Takemoto
February 27, 2018
Transcript
53 slides
-
What I talk today Think about a better way to
notify dataset changed -
notifyItemRange~() • Redraw target items • Animation • Not only
target items notifyItemRange~() -
Not only target items Adapter.notify~() A B C D B,C
is also redrawn -
notifyItem~() • Simple use of notifyItemRange~() • A bit messy
Adapter.notify~() -
adapter.getDataSet().removeAt(position) adapter.notifyItemRemoved(position) adapter.notifyItemRangeChanged( position, adapter.itemCount - position ) // ok
adapter.getDataSet().removeAt(position2) adapter.notifyItemRemoved(position2) adapter.notifyItemRangeChanged(…) // ok notifyItem~() -
Ideal • Easy to use • Redraw only changed items
• Multiple item notifying • Animation Adapter.notify~() -
DiffUtil • Support Lib 25.1.0~ • Calculate diff between 2
list • Notify only changed items • Animation DiffUtil -
DiffUtil.Callback override fun getOldListSize() = oldList.size override fun getNewListSize() =
newList.size // Same item? override fun areItemsTheSame(oldPos: Int, newPos: Int): Boolean { return oldList[oldPos].id == newList[newPos].id } // Any changes? override fun areContentsTheSame(oldPos: Int, newPos: Int): Boolean { val old = oldList[oldPos] val new = newList[newPos] return old == new } Implementation For DiffUtil -
Full Implementation val diffCallback = object : DiffUtil.Callback() { val
oldList = currentDataSet val newList = newDataSet override fun getOldListSize() = oldList.size override fun getNewListSize() = newList.size override fun areItemsTheSame(oldPos: Int, newPos: Int): Boolean = oldList[oldPos].id == newList[newPos].id override fun areContentsTheSame(oldPos: Int, newPos: Int): Boolean = oldList[oldPos] == newList[newPos] } val diffResult = DiffUtil.calculateDiff(diffCallback) diffResult.dispatchUpdatesTo(adapter) -
Calculate diff on background “If the lists are large, this
operation may take significant time” “run this on a background thread, get the DiffUtil.DiffResult then apply it on the main thread” Implementation For DiffUtil -
Calculate diff on background Thread({ val diffCallback = object :
DiffUtil.Callback() { … } // Calculate diff on background thread. val diffResult = DiffUtil.calculateDiff(diffCallback) handler.post { // Update on main thread. diffResult.dispatchUpdatesTo(adapter) } }).start() -
Calculate diff on background Thread({ val diffCallback = object :
DiffUtil.Callback() { … } // Calculate diff on background thread. val diffResult = DiffUtil.calculateDiff(diffCallback) handler.post { // Update on main thread. diffResult.dispatchUpdatesTo(adapter) } }).start() -
Calculate diff on background Thread({ val diffCallback = object :
DiffUtil.Callback() { … } // Calculate diff on background thread. val diffResult = DiffUtil.calculateDiff(diffCallback) handler.post { // Update on main thread. diffResult.dispatchUpdatesTo(adapter) } }).start() -
Paging Library Paging Library • Part of AAC • Having
same feature as DiffUtil • Paging -
Specification Implementation for Paging Library • Getting data from Github
API • Sort • Paging -
DataSource Implementation for Paging Library • Source of paged data
• Database, On Memory, API -
class PageKeyedRepoDataSource: PageKeyedDataSource<Int, Repo>() { override fun loadInitial(params: LoadInitialParams<Int>, callback:
LoadInitialCallback<Int, Repo>) { // Initial data fetching } override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, Repo>) { // no-op } override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Repo>) { // Paged data fetching } } -
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, Repo>) { val page
= 1 val nextPageKey = 2 val response = repoApiClient .getRepos(USER_NAME, page, params.requestedLoadSize) .execute() callback.onResult(response.body(), null, nextPageKey) } -
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Repo>) { val page
= params.key val nextPageKey = params.key + 1 val response = repoApiClient .getRepos(USER_NAME, page, params.requestedLoadSize) .execute() callback.onResult(response.body(), nextPageKey) } -
class RepoDataFactory: DataSource.Factory<Int, Repo> { override fun create(): DataSource<Int, Repo>
{ return object : PageKeyedDataSource<Int, Repo>() { ... } } } -
val dataSourceFactory = RepoDataFactory(repoApiClient) val config = PagedList.Config.Builder() .setInitialLoadSizeHint(10) .setPageSize(10)
.build() return LivePagedListBuilder(dataSourceFactory, config) .setBackgroundThreadExecutor(NETWORK_IO) .build() -
private fun getRepos(): LiveData<PagedList<Repo>> { ... } getRepos() .observe(context, Observer
{ pagedList -> // Items are drawn pagedListAdapter.setList(pagedList) }) Observe PagedList -
Update DataSet Implementation for Paging Library • Re-fetch API with
sort condition • Update items only changed by setting PagedList again -
private fun getRepos(condition: SortCondition) : LiveData<PagedList<Repo>> { ... } getRepos(SortCondition.UPDATED)
.observe(context, Observer { pagedList -> // Only new items are drawn repoAdapter.setList(pagedList) }) -
private fun getRepos(condition: SortCondition) : LiveData<PagedList<Repo>> { ... } getRepos(SortCondition.UPDATED)
.observe(context, Observer { pagedList -> repoAdapter.setList(null) // All new items are redrawn repoAdapter.setList(pagedList) }) -
More DataSource Implementation for Paging Library • API • Room
• Room + API • Other ← This time -
Conclusion • Consider the cost of redrawing • Use DiffUtil
• Paging Library might be better