Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

android - MotionLayout enable/disable transistion crash

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...