Share
Embed
MotionLayout & RecyclerView
One of the first and easiest things we do as Android Developers is rendering a list of items. Commonly, one of the hardest things we do next is trying to animate items in that list in a variety of ways.
Ever since Hoford & Roard showcased the possibility of animating a RecyclerView with MotionLayout a lot of people have wondered how to actually implement it. I'll be showcasing how my work has evolved in the past year in this specific vein, the issues I encountered, and new routes forward.
Jason Pearson
November 17, 2020
Transcript
128 slides
-
MotionLayout & RecyclerView November 17th, 2020 @kaeawc Jason Pearson Principal
Android Engineer @ Hinge -
0 20 40 60 80 100 ➟ Transition Motion Layout
2020 2018 Two Years Ago... -
Cancel SEND LIKE Saul Add a comment Shared Element Exclusions,
Delays, TransitionManager -
Widgets Flinging The user needs to be able to not
just swipe from one item to the next, but fling through several items. -
Flinging The user needs to be able to not just
swipe from one item to the next, but fling through several items. -
Scroll Position This introduces cases where the ViewHolder in question
is partially off screen. -
Scroll Position This introduces cases where the ViewHolder in question
is partially off screen. -
Scroll Position This introduces cases where the ViewHolder in question
is partially off screen. -
Scroll Position This introduces cases where the ViewHolder in question
is partially off screen. -
Heterogeneous Not every ViewHolder is the same size or type
of content. Some are only text, some are images and some are streaming video. -
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
</FrameLayout> activity_main.xml Component Tree -
FrameLayout <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"> </FrameLayout> activity_main.xml Component Tree -
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView android:id="@+id/widgetList" android:layout_width="match_parent" android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" /> </FrameLayout> activity_main.xml FrameLayout widgetList Component Tree -
android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/widgetList" android:layout_width="match_parent" android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" /> </FrameLayout> activity_main.xml
FrameLayout widgetList Component Tree -
android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/widgetList" android:layout_width="match_parent" android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
android:layout_height="match_parent" android:clickable="true" android:focusable="true" > </androidx.constraintlayout.widget.ConstraintLayout> </FrameLayout> activity_main.xml FrameLayout widgetList ConstraintLayout Component Tree -
android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true" > </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout> activity_main.xml FrameLayout widgetList ConstraintLayout Component Tree -
android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true" > <View
android:id="@+id/overlay" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/overlayOnSurface" /> </androidx.constraintlayout.widget.ConstraintLayout> </FrameLayout> activity_main.xml FrameLayout widgetList ConstraintLayout overlay Component Tree -
android:background="@color/overlayOnSurface" /> <include android:id="@+id/photoPlaceholder" layout="@layout/widget_photo_item" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </FrameLayout> activity_main.xml FrameLayout widgetList ConstraintLayout overlay photoPlaceholder Component Tree -
android:background="@color/overlayOnSurface" /> <include android:id="@+id/photoPlaceholder" layout="@layout/widget_photo_item" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" /> <include android:id="@+id/textPlaceholder" layout="@layout/widget_text_item" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </FrameLayout> activity_main.xml FrameLayout widgetList ConstraintLayout overlay photoPlaceholder textPlaceholder Component Tree -
android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true" > <View
android:id="@+id/overlay" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/overlayOnSurface" /> <include android:id="@+id/photoPlaceholder" layout="@layout/widget_photo_item" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> <include activity_main.xml Component Tree FrameLayout widgetList ConstraintLayout overlay photoPlaceholder textPlaceholder -
android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true" > <View
android:id="@+id/overlay" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/overlayOnSurface" /> <include android:id="@+id/photoPlaceholder" layout="@layout/widget_photo_item" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> <include activity_main.xml Component Tree FrameLayout widgetList ConstraintLayout overlay photoPlaceholder textPlaceholder -
android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true" > <View
android:id="@+id/overlay" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/overlayOnSurface" /> <include android:id="@+id/photoPlaceholder" layout="@layout/widget_photo_item" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> <include activity_main.xml Component Tree FrameLayout widgetList ConstraintLayout overlay photoPlaceholder textPlaceholder -
android:layout_height="match_parent" tools:listitem="@layout/widget_photo_item" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true" > <View
android:id="@+id/overlay" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/overlayOnSurface" /> <include android:id="@+id/photoPlaceholder" layout="@layout/widget_photo_item" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> <include activity_main.xml Component Tree FrameLayout widgetList ConstraintLayout overlay photoPlaceholder textPlaceholder -
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true" > <View android:id="@+id/overlay" android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/overlayOnSurface" /> <include android:id="@+id/photoPlaceholder" layout="@layout/widget_photo_item" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> activity_main.xml Component Tree FrameLayout widgetList ConstraintLayout overlay photoPlaceholder textPlaceholder -
<androidx.constraintlayout.motion.widget.MotionLayout android:id="@+id/constraintLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true" app:layoutDescription="@xml/activity_main_xml_constraintlayout_scene" > <View android:id="@+id/overlay"
android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/overlayOnSurface" /> <include android:id="@+id/photoPlaceholder" layout="@layout/widget_photo_item" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> activity_main.xml Component Tree FrameLayout widgetList overlay photoPlaceholder textPlaceholder constraintLayout -
activity_main.xml <androidx.constraintlayout.motion.widget.MotionLayout android:id="@+id/constraintLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true" app:layoutDescription="@xml/activity_main_xml_constraintlayout_scene" > <View
android:id="@+id/overlay" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/overlayOnSurface" /> <include android:id="@+id/photoPlaceholder" layout="@layout/widget_photo_item" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> -
<?xml version="1.0" encoding="utf-8"?> <MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:motion="http://schemas.android.com/apk/res-auto"> <Transition motion:constraintSetEnd="@id/end" motion:constraintSetStart="@id/start" motion:duration="1000"
> <KeyFrameSet> </KeyFrameSet> </Transition> <ConstraintSet android:id="@+id/start"> </ConstraintSet> <ConstraintSet android:id="@+id/end"> </ConstraintSet> main_scene.xml -
<?xml version="1.0" encoding="utf-8"?> <MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:motion="http://schemas.android.com/apk/res-auto"> <Transition motion:constraintSetEnd="@id/end" motion:constraintSetStart="@id/start" motion:duration="1000"
/> <ConstraintSet android:id="@+id/start"> </ConstraintSet> <ConstraintSet android:id="@+id/end"> </ConstraintSet> </MotionScene> main_scene.xml -
<?xml version="1.0" encoding="utf-8"?> <MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:motion="http://schemas.android.com/apk/res-auto"> <Transition motion:constraintSetStart="@id/start" motion:constraintSetEnd="@id/end" motion:duration="1000"
/> <ConstraintSet android:id="@+id/start"> </ConstraintSet> <ConstraintSet android:id="@+id/end"> </ConstraintSet> </MotionScene> main_scene.xml -
<?xml version="1.0" encoding="utf-8"?> <MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <Transition app:constraintSetStart="@id/start" app:constraintSetEnd="@id/end" app:duration="1000"
/> <ConstraintSet android:id="@+id/start"> </ConstraintSet> <ConstraintSet android:id="@+id/end"> </ConstraintSet> </MotionScene> main_scene.xml -
<?xml version="1.0" encoding="utf-8"?> <MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <Transition app:constraintSetStart="@id/list" app:constraintSetEnd="@id/detail" app:duration="1000"
/> <ConstraintSet android:id="@+id/list"> </ConstraintSet> <ConstraintSet android:id="@+id/detail"> </ConstraintSet> </MotionScene> main_scene.xml -
<ConstraintSet android:id="@+id/list"> </ConstraintSet> <ConstraintSet android:id="@+id/detail"> <Constraint android:id="@id/photoPlaceholder"> <Layout android:layout_width="match_parent" android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.35" /> <Transform android:scaleX="0.9" android:scaleY="0.9" /> <PropertySet app:visibilityMode="ignore" /> </Constraint> <Constraint android:id="@id/textPlaceholder" .../> </ConstraintSet> </MotionScene> main_scene.xml -
<ConstraintSet android:id="@+id/list"> </ConstraintSet> <ConstraintSet android:id="@+id/detail"> <Constraint android:id="@id/photoPlaceholder"> <Layout android:layout_width="match_parent" android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.35" /> <Transform android:scaleX="0.9" android:scaleY="0.9" /> <PropertySet app:visibilityMode="ignore" /> </Constraint> <Constraint android:id="@id/textPlaceholder" .../> </ConstraintSet> </MotionScene> main_scene.xml -
0 20 40 60 80 100 ➟ Transition Motion Layout
end start activity_main.xml main_scene.xml -
0 20 40 60 80 100 ➟ Transition Motion Layout
end start activity_main.xml main_scene.xml -
class MainActivity : AppCompatActivity(R.layout.activity_main) { private val ui: ActivityMainBinding by
lazy { ... } private var clicks = Channel<Int>(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 -
private var clicks = Channel<Int>(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 -
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<WidgetViewHolder<*>>() .firstOrNull { it.adapterPosition == position } } private fun onWidgetTapped(viewHolder: WidgetViewHolder<*>?) { if (viewHolder == null) return MainActivity.kt -
.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 -
} 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 -
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 -
fun <T : ViewBinding> 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 -
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 -
// 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 -
// Create Bitmap from source Canvas // TODO: Very inefficient,
please fix placeholder.applyBindingToPlaceholder(source) Assessment -
Use RecyclerView internals to detach and use ViewHolders in MotionLayout
temporarily Next Idea... -
LayoutManager detaching views ✅ No delay to transition ✅ Reusable
✅ Efficient ❌ Probably can't work with Exoplayer ❌ RecyclerView dependency -
fun <T : ViewBinding> 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 -
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 -
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 -
Motion Layout Assessment ✅ No delay to transition ✅ Robust
✅ Reusable Efficient? -
Motion Layout Assessment ✅ No delay to transition ✅ Robust
✅ Reusable ✅ Efficient enough for now