Skip to content

Commit 19fdff2

Browse files
committed
Saving state of motion layout
1 parent 0b13d81 commit 19fdff2

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.bgrebennikov.devbuff.presentation.ui.customViews
2+
3+
import android.content.Context
4+
import android.os.Parcelable
5+
import android.util.AttributeSet
6+
import androidx.constraintlayout.motion.widget.MotionLayout
7+
import kotlinx.parcelize.Parcelize
8+
9+
class SavingMotionLayout @JvmOverloads constructor(
10+
context: Context,
11+
attrs: AttributeSet?,
12+
defStyleAttr: Int = 0
13+
) : MotionLayout(
14+
context, attrs, defStyleAttr
15+
) {
16+
17+
override fun onSaveInstanceState(): Parcelable {
18+
return SaveState(super.onSaveInstanceState(), startState, endState, targetPosition)
19+
}
20+
21+
override fun onRestoreInstanceState(state: Parcelable?) {
22+
(state as? SaveState)?.let {
23+
super.onRestoreInstanceState(it.superParcel)
24+
setTransition(it.startState, it.endState)
25+
progress = it.progress
26+
}
27+
}
28+
29+
@Parcelize
30+
private class SaveState(
31+
val superParcel: Parcelable?,
32+
val startState: Int,
33+
val endState: Int,
34+
val progress: Float
35+
) : Parcelable
36+
37+
}

app/src/main/res/layout/fragment_idea_details.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818

1919
</data>
2020

21-
<androidx.constraintlayout.motion.widget.MotionLayout
21+
<com.github.bgrebennikov.devbuff.presentation.ui.customViews.SavingMotionLayout
2222
android:id="@+id/constraintLayout3"
2323
android:layout_width="match_parent"
2424
android:layout_height="match_parent"
2525
android:background="@color/white"
2626
app:layoutDescription="@xml/fragment_idea_details_xml_scene"
2727
tools:context=".presentation.ui.fragments.IdeaDetailsFragment"
28+
android:saveEnabled="true"
2829
>
2930

3031

@@ -161,5 +162,5 @@
161162

162163

163164

164-
</androidx.constraintlayout.motion.widget.MotionLayout>
165+
</com.github.bgrebennikov.devbuff.presentation.ui.customViews.SavingMotionLayout>
165166
</layout>

0 commit comments

Comments
 (0)