Challenge: Efficient RecyclerView Reloading

Right now, when the user returns to the list screen after editing a single crime, CrimeListFragment redraws all of the visible crimes in the recycler view. The is wildly inefficient, because at most one Crime will have changed.

Update CrimeListFragment’s RecyclerView implementation to redraw only the row associated with the changed crime. To do this, update CrimeAdapter to extend from androidx.recyclerview.widget.ListAdapter<Crime, CrimeHolder> instead of RecyclerView.Adapter<CrimeHolder>.

ListAdapter is a RecyclerView.Adapter that figures out the difference between the current data set backing the recycler view and a new data set you apply to back the recycler view. The comparison happens on a background thread, so it does not slow down the UI. ListAdapter in turn tells the recycler view to only redraw rows for data that has changed.

ListAdapter uses androidx.recyclerview.widget.DiffUtil to determine which parts of the data set have changed. To finish this challenge, you will need to provide a DiffUtil.ItemCallback<Crime> implementation to your ListAdapter.

You will also need to update CrimeListFragment to submit an updated crime list to the recycler view’s adapter, rather than reassigning the recycler view’s adapter to a new adapter object every time you want to update the UI. You can submit a new list by calling ListAdapter.submitList(MutableList<T>?). Or you can set up LiveData and observe the changes.

See the API reference pages for androidx.recyclerview.widget.DiffUtil and androidx.recyclerview.widget.ListAdapter on developer.android.com/​reference/​kotlin for more details on how to use these tools.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
44.200.179.138