2017.03.20 android taipei
TRANSCRIPT
![Page 1: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/1.jpg)
淺談 Diffutil RecyclerViewChocolabs Android Developer
Louis
2017.3.20 Android Taipei
![Page 2: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/2.jpg)
RecyclerView can be improved funter ?
adapter.notifyDataSetChanged();
![Page 3: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/3.jpg)
SummaryA. DiffUtil is a utility class that can calculate the difference between two lists and output a
list of update operations that converts the first list into the second one. It can be used to calculate updates for a RecyclerView Adapter.
B. DiffUtil uses Eugene W. Myers's difference algorithm to calculate the minimal number of updates to convert one list into another. Myers's algorithm does not handle items that are moved so DiffUtil runs a second pass on the result to detect items that were moved.
C. If the lists are large, this operation may take significant time so you are advised to run this on a background thread, get the DiffUtil.DiffResult then apply it on the RecyclerView on the main thread.
![Page 4: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/4.jpg)
AlgorithmA. This algorithm is optimized for space and uses O(N) space to find the minimal number
of addition and removal operations between the two lists. It has O(N + D^2) expected time performance where D is the length of the edit script.
B. If move detection is enabled, it takes an additional O(N^2) time where N is the total number of added and removed items. If your lists are already sorted by the same constraint (e.g. a created timestamp for a list of posts), you can disable move detection to improve performance.
a. 100 items and 10 modifications: avg: 0.39 ms, median: 0.35 ms
b. 100 items and 100 modifications: 3.82 ms, median: 3.75 ms
c. 100 items and 100 modifications without moves: 2.09 ms, median: 2.06 ms
d. 1000 items and 50 modifications: avg: 4.67 ms, median: 4.59 ms
e. 1000 items and 50 modifications without moves: avg: 3.59 ms, median: 3.50 ms
f. 1000 items and 200 modifications: 27.07 ms, median: 26.92 ms
g. 1000 items and 200 modifications without moves: 13.54 ms, median: 13.36 ms
C. Due to implementation constraints, the max size of the list can be 2^26.
![Page 5: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/5.jpg)
![Page 6: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/6.jpg)
extends DiffUtil.Callback
![Page 7: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/7.jpg)
Implementation
![Page 8: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/8.jpg)
![Page 9: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/9.jpg)
Do we really need this …?
![Page 10: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/10.jpg)
For some caseIf u want to handle the animation If u just only need to consider what
exactly data change between two list(same length)
![Page 11: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/11.jpg)
Thanks
![Page 12: 2017.03.20 android taipei](https://reader035.vdocuments.mx/reader035/viewer/2022070523/58ed49571a28abf47d8b45fd/html5/thumbnails/12.jpg)
Reference
https://medium.com/wiselteach/diffutils-improving-performance-of-recyclerview-102b254a9e4a#.4v1o7u22c
https://developer.android.com/reference/android/support/v7/util/DiffUtil.html