问题描述
我正在尝试使用类似火种的滑动和点击的翻转动画来创建“抽认卡应用程序”。 为了实现这一点,我有两个片段,一个用于卡片的正面,一个用于卡片的背面。要制作翻转动画,我使用此代码从前向后翻转:
val ft = supportFragmentManager
ft
.beginTransaction()
.setCustomAnimations(R.anim.right_in,R.anim.right_out)
.replace(R.id.myFrame,SecondFragment())
.commit()
从后到前:
ft
.beginTransaction()
.setCustomAnimations(R.anim.left_in,R.anim.left_out)
.replace(R.id.myFrame,FirstFragment())
.commit()
对于滑动动画,我使用的是带有 onSwipe 的motionLayout:
<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>
1)当我翻转卡片然后刷卡时,我使用这两种方法:
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()
}
}
}
问题是,如果我快速从前向后翻转然后滑动,应用程序会因为找不到前面的片段而崩溃。我使用 viewmodel swipe() 函数和 LiveData 来更新当前卡片内容 (FirstFragment)。 所以我试图停止滑动动画,直到卡片没有翻转。 我试过使用:
motionLayout.enableTransition(R.id.pass,false)
或者:
motionLayout.getTransition(R.id.pass).setEnable(false)
但是应用程序由于空指针异常而崩溃(我认为系统没有找到 pass id)。 有什么建议吗?
在开发过程中,我尝试了不同的解决方案,但发现了一些类似的问题:
1)如果我在 OnSwipe 的同一视图上使用 setonclickListener 或 setonTouchListener,则滑动不再起作用。我已经使用按钮翻转和 OnSwipe 滑动解决了这个问题。
2) 如果我使用 ScrollView(甚至是 nestedScrollView),则 OnSwipe 不起作用。我将使用另一个按钮来解决此问题,该按钮将展开卡片,并添加另一个带有 ScrollView 的全屏片段
我已经解决了这两个问题,所以这只是好奇,但在没有按钮的情况下在同一个视图上点击/滚动/滑动可能很棒。
谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)