I'm trying to create an "flashcard app" with a tinder-like swipe and a flip animation with a click.
To achive this I have two fragments, one for the front and on for the back of the card. To do the flip animation I'm using this code to flip from front to back:
val ft = supportFragmentManager
ft
.beginTransaction()
.setCustomAnimations(R.anim.right_in, R.anim.right_out)
.replace(R.id.myFrame, SecondFragment())
.commit()
And this from back to front:
ft
.beginTransaction()
.setCustomAnimations(R.anim.left_in, R.anim.left_out)
.replace(R.id.myFrame, FirstFragment())
.commit()
For the swipe animation I'm using motionLayout with onSwipe:
<MotionScene
...
<Transition
motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/learnt"
motion:duration="10">
<OnSwipe
motion:dragDirection="dragRight"
motion:touchAnchorId="@id/myFrame"
motion:touchAnchorSide="right"
motion:onTouchUp="decelerateAndComplete"
motion:touchRegionId="@id/myFrame" />
</Transition>
<ConstraintSet
android:id="@+id/backToStart"
motion:deriveConstraintsFrom="@+id/learnt" />
<Transition
motion:constraintSetStart="@+id/backToStart"
motion:constraintSetEnd="@+id/start"
motion:autoTransition="jumpToEnd" />
<Transition
motion:constraintSetStart="@+id/start"
motion:constraintSetEnd="@+id/pass">
<OnSwipe motion:dragDirection="dragLeft"
motion:onTouchUp="decelerateAndComplete"
motion:touchAnchorId="@id/myFrame"
motion:touchAnchorSide="left"
motion:touchRegionId="@id/myFrame" />
</Transition>
I have one big issue:
1)When I flip a card and then swipe, I use these two method:
override fun onTransitionChange(motionLayout: MotionLayout?,
startId: Int, endId: Int, progress: Float) {
when(startId){
R.id.start -> {
if(progress > 0.8f){
if(back){
back = false
val ft = supportFragmentManager
ft
.beginTransaction()
.replace(R.id.myFrame, FirstFragment())
.commit()
}
}
}
}
}
override fun onTransitionCompleted(motionLayout: MotionLayout?,
currentId: Int) {
when(currentId){
R.id.learnt, R.id.pass -> {
motionLayout?.progress = 0f
motionLayout?.transitionToState(R.id.backToStart)
motionLayout?.transitionToEnd()
viewModel.swipe()
}
}
}
The problem is that if I quickly flip from front to back then swipe, the app crash because the front fragment is not found. I use a ViewModel swipe() function with LiveData to update the current card contents (FirstFragment).
So I'm trying to stop the swipe animation until the card is not flipped.
I've tried to use:
motionLayout.enableTransition(R.id.pass, false)
Or:
motionLayout.getTransition(R.id.pass).setEnable(false)
But the app crash due a null pointer exception (I think that the system doesn't find the pass id).
Any suggestion?
While I'm developing, I've tried different solution and I've found some problems like these:
1)If I use setOnclickListener or setOnTouchListener on the same view of the OnSwipe, the swipe doesn't work anymore. I've resolved this using a button to flip and OnSwipe to swipe.
2)If I use a ScrollView (even a NestedScrollView) the OnSwipe doesn't work. I will resolve this problem with another button that will expand the card and I will add another full screen fragment with a ScrollView
I've resolved these two problems as written, so It's just curiosity, but could be great click/scroll/swipe on the same view without buttons.
Thanks.
question from:
https://stackoverflow.com/questions/65887519/motionlayout-enable-disable-transistion-crash