Slide 1

Slide 1 text

MotionLayout & RecyclerView November 17th, 2020 @kaeawc Jason Pearson Principal Android Engineer @ Hinge

Slide 2

Slide 2 text

Journey

Slide 3

Slide 3 text

Journey

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Widgets

Slide 7

Slide 7 text

Widgets

Slide 8

Slide 8 text

Widgets

Slide 9

Slide 9 text

Widgets

Slide 10

Slide 10 text

Widgets

Slide 11

Slide 11 text

0 20 40 60 80 100 ➟ Transition Motion Layout 2020 2018

Slide 12

Slide 12 text

0 20 40 60 80 100 ➟ Transition Motion Layout 2020 2018 Two Years Ago...

Slide 13

Slide 13 text

CoordinatorLayout + RecyclerView Complicated Layout Cancel SEND LIKE Saul Add a comment

Slide 14

Slide 14 text

CoordinatorLayout + RecyclerView Complicated Layout Cancel SEND LIKE Saul Add a comment

Slide 15

Slide 15 text

Cancel SEND LIKE Saul Add a comment Shared Element Exclusions, Delays, TransitionManager

Slide 16

Slide 16 text

Hacky + Limited

Slide 17

Slide 17 text

Hacky + Limited Need to Animate Even More

Slide 18

Slide 18 text

So... what to do?

Slide 19

Slide 19 text

Conventional wisdom said we're animating too much

Slide 20

Slide 20 text

Is Android just not good enough?

Slide 21

Slide 21 text

Existential doubt

Slide 22

Slide 22 text

MotionLayout

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Coordination Gesture Support Interruptible Scrolling Support Ticked All the Boxes ✅ ✅ ✅ ✅

Slide 26

Slide 26 text

So how do I build this?

Slide 27

Slide 27 text

First let's look at requirements

Slide 28

Slide 28 text

Configuration Changes Widgets

Slide 29

Slide 29 text

Configuration Changes Widgets

Slide 30

Slide 30 text

Configuration Changes Widgets

Slide 31

Slide 31 text

Widgets Configuration Changes

Slide 32

Slide 32 text

Widgets Configuration Changes Typing something interesting

Slide 33

Slide 33 text

Widgets Flinging The user needs to be able to not just swipe from one item to the next, but fling through several items.

Slide 34

Slide 34 text

Flinging The user needs to be able to not just swipe from one item to the next, but fling through several items.

Slide 35

Slide 35 text

Scroll Position

Slide 36

Slide 36 text

Scroll Position This introduces cases where the ViewHolder in question is partially off screen.

Slide 37

Slide 37 text

Scroll Position This introduces cases where the ViewHolder in question is partially off screen.

Slide 38

Slide 38 text

Scroll Position This introduces cases where the ViewHolder in question is partially off screen.

Slide 39

Slide 39 text

Scroll Position This introduces cases where the ViewHolder in question is partially off screen.

Slide 40

Slide 40 text

Heterogeneous

Slide 41

Slide 41 text

Heterogeneous Not every ViewHolder is the same size or type of content. Some are only text, some are images and some are streaming video.

Slide 42

Slide 42 text

Solutions

Slide 43

Slide 43 text

Evolving Approaches

Slide 44

Slide 44 text

Evolving Approaches

Slide 45

Slide 45 text

Create placeholder by hand Evolving Approaches

Slide 46

Slide 46 text

widget_photo_item.xml

Slide 47

Slide 47 text

widget_text_item.xml

Slide 48

Slide 48 text

activity_main.xml Component Tree

Slide 49

Slide 49 text

FrameLayout activity_main.xml Component Tree

Slide 50

Slide 50 text

activity_main.xml FrameLayout widgetList Component Tree

Slide 51

Slide 51 text

android:layout_height="match_parent" tools:context=".MainActivity"> activity_main.xml FrameLayout widgetList Component Tree

Slide 52

Slide 52 text

android:layout_height="match_parent" tools:context=".MainActivity"> activity_main.xml FrameLayout widgetList ConstraintLayout Component Tree

Slide 53

Slide 53 text

android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" /> activity_main.xml FrameLayout widgetList ConstraintLayout Component Tree

Slide 54

Slide 54 text

android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" /> activity_main.xml FrameLayout widgetList ConstraintLayout overlay Component Tree

Slide 55

Slide 55 text

android:background="@color/overlayOnSurface" /> activity_main.xml FrameLayout widgetList ConstraintLayout overlay photoPlaceholder Component Tree

Slide 56

Slide 56 text

android:background="@color/overlayOnSurface" /> activity_main.xml FrameLayout widgetList ConstraintLayout overlay photoPlaceholder textPlaceholder Component Tree

Slide 57

Slide 57 text

android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" />

Slide 58

Slide 58 text

android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" />

Slide 59

Slide 59 text

android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" />

Slide 60

Slide 60 text

android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" />

Slide 61

Slide 61 text

activity_main.xml Component Tree FrameLayout widgetList ConstraintLayout overlay photoPlaceholder textPlaceholder

Slide 62

Slide 62 text

activity_main.xml Component Tree FrameLayout widgetList overlay photoPlaceholder textPlaceholder constraintLayout

Slide 63

Slide 63 text

activity_main.xml

Slide 64

Slide 64 text

activity_main.xml

Slide 65

Slide 65 text

activity_main.xml

Slide 66

Slide 66 text

activity_main.xml

Slide 67

Slide 67 text

activity_main.xml

Slide 68

Slide 68 text

activity_main.xml

Slide 69

Slide 69 text

activity_main.xml

Slide 70

Slide 70 text

activity_main.xml

Slide 71

Slide 71 text

activity_main.xml

Slide 72

Slide 72 text

activity_main.xml

Slide 73

Slide 73 text

main_scene.xml

Slide 74

Slide 74 text

main_scene.xml

Slide 75

Slide 75 text

main_scene.xml

Slide 76

Slide 76 text

main_scene.xml

Slide 77

Slide 77 text

main_scene.xml

Slide 78

Slide 78 text

main_scene.xml

Slide 79

Slide 79 text

main_scene.xml

Slide 80

Slide 80 text

main_scene.xml

Slide 81

Slide 81 text

main_scene.xml activity_main.xml

Slide 82

Slide 82 text

Motion Layout end start activity_main.xml main_scene.xml

Slide 83

Slide 83 text

0 20 40 60 80 100 ➟ Transition Motion Layout end start activity_main.xml main_scene.xml

Slide 84

Slide 84 text

0 20 40 60 80 100 ➟ Transition Motion Layout end start activity_main.xml main_scene.xml

Slide 85

Slide 85 text

Motion Layout activity_main.xml end start main_scene.xml

Slide 86

Slide 86 text

activity_main.xml android:visibility="gone" > main_scene.xml

Slide 87

Slide 87 text

class MainActivity : AppCompatActivity(R.layout.activity_main) { private val ui: ActivityMainBinding by lazy { ... } private var clicks = Channel(1) private var placeholderSource: ViewBinding? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val view = ui.root setContentView(view) ui.widgetList.adapter = WidgetAdapter(clicks) } override fun onResume() { super.onResume() MainActivity.kt

Slide 88

Slide 88 text

private var clicks = Channel(1) private var placeholderSource: ViewBinding? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val view = ui.root setContentView(view) ui.widgetList.adapter = WidgetAdapter(clicks) } override fun onResume() { super.onResume() clicks.consumeAsFlow() .onEach { onWidgetTapped(getWidgetViewHolder(it)) } .launchIn(lifecycleScope) } private fun getWidgetViewHolder(position: Int): MainActivity.kt

Slide 89

Slide 89 text

ui.widgetList.adapter = WidgetAdapter(clicks) } override fun onResume() { super.onResume() clicks.consumeAsFlow() .onEach { onWidgetTapped(getWidgetViewHolder(it)) } .launchIn(lifecycleScope) } private fun getWidgetViewHolder(position: Int): WidgetViewHolder<*>? { return ui.widgetList.activeViewHolders(onlyVisible = true) .filterIsInstance>() .firstOrNull { it.adapterPosition == position } } private fun onWidgetTapped(viewHolder: WidgetViewHolder<*>?) { if (viewHolder == null) return MainActivity.kt

Slide 90

Slide 90 text

.firstOrNull { it.adapterPosition == position } } private fun onWidgetTapped(viewHolder: WidgetViewHolder<*>?) { if (viewHolder == null) return val placeholder = when (viewHolder) { is WidgetPhotoViewHolder -> { ui.textPlaceholder.root.isVisible = false ui.photoPlaceholder } is WidgetTextViewHolder -> { ui.photoPlaceholder.root.isVisible = false ui.textPlaceholder } else -> return } ui.motionLayout.enterPlaceholder( MainActivity.kt

Slide 91

Slide 91 text

} else -> return } ui.motionLayout.enterPlaceholder( listView = ui.widgetList, source = viewHolder.binding, placeholder = placeholder, startState = R.id.list, endState = R.id.detail) placeholderSource = viewHolder.binding } override fun onBackPressed() { val source = placeholderSource if (source != null) { ui.motionLayout.exitPlaceholder(source) { placeholderSource = null } MainActivity.kt

Slide 92

Slide 92 text

placeholderSource = viewHolder.binding } override fun onBackPressed() { val source = placeholderSource if (source != null) { ui.motionLayout.exitPlaceholder(source) { placeholderSource = null } ui.motionLayout.transitionToState(R.id.list) } else { super.onBackPressed() } } } MainActivity.kt

Slide 93

Slide 93 text

fun MotionLayout.enterPlaceholder( listView: View, source: T, placeholder: T, startState: Int, endState: Int, completion: () -> Unit = {}) { // Create Bitmap from source Canvas // TODO: Very inefficient, please fix placeholder.applyBindingToPlaceholder(source) // Updates starting state with constraints that match the source val profileState = calculatePlaceholderConstraints(placeholder.root.id, source, listView) updateState(startState, profileState) setTransition(startState, endState) isVisible = true placeholder.root.isVisible = true MotionExtensions.kt MainActivity.kt

Slide 94

Slide 94 text

startState: Int, endState: Int, completion: () -> Unit = {}) { // Create Bitmap from source Canvas // TODO: Very inefficient, please fix placeholder.applyBindingToPlaceholder(source) // Updates starting state with constraints that match the source val profileState = calculatePlaceholderConstraints(placeholder.root.id, source, listView) updateState(startState, profileState) setTransition(startState, endState) isVisible = true placeholder.root.isVisible = true after(completion) transitionToEnd() source.root.alpha = 0f } MotionExtensions.kt MainActivity.kt

Slide 95

Slide 95 text

// Create Bitmap from source Canvas // TODO: Very inefficient, please fix placeholder.applyBindingToPlaceholder(source) // Updates starting state with constraints that match the source val profileState = calculatePlaceholderConstraints(placeholder.root.id, source, listView) updateState(startState, profileState) setTransition(startState, endState) isVisible = true placeholder.root.isVisible = true after(completion) transitionToEnd() source.root.alpha = 0f } MotionExtensions.kt MainActivity.kt

Slide 96

Slide 96 text

Assessment

Slide 97

Slide 97 text

✅ No delay to transition Assessment

Slide 98

Slide 98 text

Assessment

Slide 99

Slide 99 text

// Create Bitmap from source Canvas // TODO: Very inefficient, please fix placeholder.applyBindingToPlaceholder(source) Assessment

Slide 100

Slide 100 text

Use RecyclerView internals to detach and use ViewHolders in MotionLayout temporarily Next Idea...

Slide 101

Slide 101 text

LayoutManager detaching views ✅ No delay to transition ✅ Reusable ✅ Efficient

Slide 102

Slide 102 text

LayoutManager detaching views ✅ No delay to transition ✅ Reusable ✅ Efficient ❌ Probably can't work with Exoplayer ❌ RecyclerView dependency

Slide 103

Slide 103 text

Use MotionLayout internals for attribute copying Next Idea

Slide 104

Slide 104 text

fun MotionLayout.enterPlaceholder( listView: View, source: T, placeholder: T, startState: Int, endState: Int, completion: () -> Unit = {}) { // Create Bitmap from source Canvas // TODO: Very inefficient, please fix placeholder.applyBindingToPlaceholder(source) // Updates starting state with constraints that match the source val profileState = calculatePlaceholderConstraints(placeholder.root.id, source, listView) updateState(startState, profileState) setTransition(startState, endState) isVisible = true placeholder.root.isVisible = true MotionExtensions.kt

Slide 105

Slide 105 text

source: T, placeholder: T, startState: Int, endState: Int, completion: () -> Unit = {}) { // Create Bitmap from source Canvas // TODO: Very inefficient, please fix placeholder.applyBindingToPlaceholder(source) // Updates starting state with constraints that match the source val profileState = calculatePlaceholderConstraints(placeholder.root.id, source, listView) updateState(startState, profileState) setTransition(startState, endState) isVisible = true placeholder.root.isVisible = true MotionExtensions.kt

Slide 106

Slide 106 text

source: T, placeholder: T, startState: Int, endState: Int, completion: () -> Unit = {}) { // Copy attributes from source placeholder.applyBindingToPlaceholder(source) // Updates starting state with constraints that match the source val profileState = calculatePlaceholderConstraints(placeholder.root.id, source, listView) updateState(startState, profileState) setTransition(startState, endState) isVisible = true placeholder.root.isVisible = true after(completion) MotionExtensions.kt

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

Assessment

Slide 112

Slide 112 text

Assessment ✅ No delay to transition ✅ Robust ✅ Reusable Efficient?

Slide 113

Slide 113 text

Motion Layout Assessment ✅ No delay to transition ✅ Robust ✅ Reusable Efficient?

Slide 114

Slide 114 text

Motion Layout Assessment ✅ No delay to transition ✅ Robust ✅ Reusable ✅ Efficient enough for now

Slide 115

Slide 115 text

Motion Layout

Slide 116

Slide 116 text

Motion Layout

Slide 117

Slide 117 text

Motion Layout

Slide 118

Slide 118 text

Motion Layout grind

Slide 119

Slide 119 text

Motion Layout grind

Slide 120

Slide 120 text

Motion Layout grind

Slide 121

Slide 121 text

Motion Layout grind

Slide 122

Slide 122 text

Motion Layout grind

Slide 123

Slide 123 text

Motion Layout grind

Slide 124

Slide 124 text

Motion Layout grind

Slide 125

Slide 125 text

Motion Layout fun grind

Slide 126

Slide 126 text

Motion Layout fun grind

Slide 127

Slide 127 text

Questions? @kaeawc Slides will be posted: https://bit.ly/2Uxq4VU

Slide 128

Slide 128 text

http://bit.ly/dcsf19-adv-motionlayout Questions? @kaeawc