Upgrade to Pro — share decks privately, control downloads, hide ads and more …

recycler_view.pdf

Vlad
March 25, 2019

 recycler_view.pdf

Vlad

March 25, 2019
Tweet

More Decks by Vlad

Other Decks in Technology

Transcript

  1. Agenda Tips & Tricks RecyclerView components ListView Icons made by

    Freepik from Flaticon are licensed by CC 3.0 BY
  2. • complexity overdraft • duplicate features • animations • more

    complex layouts: GridView, Horizontal ListView, StaggeredGridView ListView Icons made by Freepik from Flaticon are licensed by CC 3.0 BY
  3. Adapter • Create View and ViewHolder • Bind an item

    to a holder • Notify RecyclerView about changes • Notify data out of sync
  4. Adapter • item interactions handling (clicks etc.) • multiple view

    types • recycler recovery (onFailedToRecycleView) • granular data change events
  5. Recycler • Manage scrapped or detached item views for reuse

    • A "scrapped" view is a view that is still attached to its parent RecyclerView but that has been marked for removal or reuse. Recycled View Pool
  6. Recycler Recycled View Pool • Sanctuary for reserve ViewHolders •

    Can be shared between RecyclerViews or custom view groups RecyclerView.setRecycledViewPool(RecycledViewPool) • Per Activity context
  7. LayoutManager Adapter Recycled View Pool Cache ViewHolder — Birth RecyclerView

    getViewForPosition getViewForPosition getViewType getViewHolderByType createViewHolder bindViewHolder
  8. LayoutManager Adapter Recycled View Pool Cache RecyclerView removeAndRecycleView onViewDetachedFromWindow ViewHolder

    — Reserve Is Valid? recycle
 (oldest cache) onViewRecycled
 (oldest cache)
  9. ViewHolder — Reserve private void scrapOrRecycleView(Recycler recycler, int index, View

    view) { final ViewHolder viewHolder = getChildViewHolderInt(view); if (viewHolder.shouldIgnore()) { if (DEBUG) { Log.d(TAG, "ignoring view " + viewHolder); } return; } if (viewHolder.isInvalid() && !viewHolder.isRemoved() && !mRecyclerView.mAdapter.hasStableIds()) { removeViewAt(index); recycler.recycleViewHolderInternal(viewHolder); } else { detachViewAt(index); recycler.scrapView(view); mRecyclerView.mViewInfoStore.onViewDetached(viewHolder); } } Is Valid?
  10. Recycler LayoutManager Adapter Recycled View Pool Scrap Heap RecyclerView onLayoutChildren

    ViewHolder — Fancy Reserve Re add to ViewGroup hide from LM for each disappearing child animate child onAnimationFinished onViewDetachedFromWindow cache or recycle ItemAnimator
  11. Recycled View Pool RecyclerView ViewHolder — Death 2 Is there

    room for another VH type X? add view to pool Icon made by Freepik from Flaticon are licensed by CC 3.0 BY
  12. ViewHolder — Death 2 Icon made by Freepik from Flaticon

    are licensed by CC 3.0 BY Why it didn’t cache? • Too many ViewHolders of the same type How come? • Animate cross fade for all views • notifyItemChanged(0, getItemCount)) How to fix? • Granular updates (notifyDataSetChanged(3)) • pool.setMaxRecycledViews(type, count)
  13. Item Decorations • Can add offset to view bounds •

    Custom drawing on RecyclerView canvas • Can have multiple ItemDecoration
  14. Item Decorations • Do NOT try to access the adapter

    • Keep necessary information in View Holder • General onDraw rules apply (don’t allocate etc.) • recyclerView.getChildViewHolder(view)
  15. Child Helper Provide a virtual children list to the LayoutManager

    Brest Vitebsk Gomel Grodno Mogilev Minsk layoutManager.getChildAt(3) = recyclerView.getChildAt(3) = Grodno Mogilev Mogilev
  16. Adapter Helper Brest Vitebsk Gomel Grodno Mogilev Minsk 25 26

    27 28 29 30 ADD (25, 1) RM (31, 2) viewHolder.getAdapterPosition() position: 29 30 MV (28, 27)
  17. No Update = no onBind Items are not rebound unless

    updated adapter.notifyItemMoved(2, 7) 0 onBind calls
  18. onBind position != final position fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int)

    { holder.itemView.setOnClickListener { removeAtPosition(position) } } BUG: onBindViewHolder(holder, 5) notifyItemMoved(5, 15) holder.itemView.callOnClick()
  19. onBind position != final position fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int)

    { holder.itemView.setOnClickListener { removeAtPosition(holder.adapterPosition) } }
  20. Positions: Adapter vs Layout Brest Vitebsk Gomel Grodno Mogilev Minsk

    0 1 2 3 4 5 0 1 2 3 4 5 AP LP move(2, 5) 0 1 5 2 3 4 0 1 2 3 4 5 AP LP onLayout
  21. Positions: Adapter vs Layout Brest Vitebsk Grodno Mogilev Minsk Gomel

    0 1 2 3 4 5 0 1 2 3 4 5 AP LP move(2, 5) 0 1 5 2 3 4 0 1 2 3 4 5 AP LP onLayout
  22. Icons made by Freepik from Flaticon are licensed by CC

    3.0 BY Drag and Swipe ItemTouchHelper - supports both swipe and drag, together if you please - per ViewHolder val callback = SimpleItemTouchHelperCallback(adapter) val touchHelper = ItemTouchHelper(callback) touchHelper.attachToRecyclerView(recyclerView) https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf
  23. Icons made by Freepik from Flaticon are licensed by CC

    3.0 BY DiffUtil • calculates the difference between two given lists • O(N) space to find adds and removes • O(N + D^2) time • plus O(N^2) time to detect moves random UUID Strings, Nexus 5X with M: • 100 items and 10 modifications: avg: 0.39 ms, median: 0.35 ms • 100 items and 100 modifications: 3.82 ms, median: 3.75 ms • 100 items and 100 modifications without moves: 2.09 ms, median: 2.06 ms • 1000 items and 50 modifications: avg: 4.67 ms, median: 4.59 ms • 1000 items and 50 modifications without moves: avg: 3.59 ms, median: 3.50 ms • 1000 items and 200 modifications: 27.07 ms, median: 26.92 ms • 1000 items and 200 modifications without moves: 13.54 ms, median: 13.36 ms https://developer.android.com/reference/android/support/v7/util/DiffUtil.html
  24. Icons made by Freepik from Flaticon are licensed by CC

    3.0 BY RecyclerView Prefetch https://medium.com/google-developers/recyclerview-prefetch-c2f269075710
  25. Icons made by Freepik from Flaticon are licensed by CC

    3.0 BY RecyclerView Prefetch https://medium.com/google-developers/recyclerview-prefetch-c2f269075710
  26. Icons made by Freepik from Flaticon are licensed by CC

    3.0 BY RecyclerView Prefetch https://medium.com/google-developers/recyclerview-prefetch-c2f269075710
  27. Icons made by Freepik from Flaticon are licensed by CC

    3.0 BY RecyclerView Prefetch https://medium.com/google-developers/recyclerview-prefetch-c2f269075710
  28. Icons made by Freepik from Flaticon are licensed by CC

    3.0 BY RecyclerView Prefetch https://medium.com/google-developers/recyclerview-prefetch-c2f269075710
  29. Icons made by Freepik from Flaticon are licensed by CC

    3.0 BY PrecomputedText API in Android Pie https://medium.com/mindorks/precomputedtext-new-api-in-android-pie-74eb8f420ee6
  30. Icons made by Freepik from Flaticon are licensed by CC

    3.0 BY Prefetch Text Layout in RecyclerView https://medium.com/androiddevelopers/prefetch-text-layout-in-recyclerview-4acf9103f438
  31. Icons made by Freepik from Flaticon are licensed by CC

    3.0 BY More info • RecyclerView ins and outs by Adam Powell & Yigit Boyar — https://youtu.be/LqBlYJTfLP4 • Pro RecyclerView by Yigit Boyar— https://youtu.be/KhLVD6iiZQs • Radical RecyclerView by Lisa Wray — https://youtu.be/TS_J0Qw4zl0 • 032. RecyclerView 13000 by Артур Василов — https://youtu.be/860_Y5TXh1Y
  32. [email protected] Icons made by Freepik from Flaticon are licensed by

    CC 3.0 BY https://vladds.com/recycler_view.pdf