[英]How to use Fragment transition on Android 8 (API 26, Oreo)
I am using a FragmentManager to replace one fragment with another with a shared element transition (two elements are shared). 我正在使用FragmentManager通过共享元素转换(共享两个元素)将一个片段替换为另一个片段。 Here is the code (in Kotlin):
这是代码(在Kotlin中):
private fun showList(edited: EditedAlarm) {
fragmentManager.findFragmentById(R.id.main_fragment_container)?.apply{
exitTransition = Fade()
}
// create a new Fragment and set transitions
val listFragment = AlarmsListFragment().apply {
sharedElementEnterTransition = moveTransition()
enterTransition = Fade()
}
fragmentManager.beginTransaction()
.addSharedElement(viewHolder.digitalClock(), "clock" + viewHolder.alarmId())
.addSharedElement(viewHolder.container(), "onOff" + viewHolder.alarmId())
.replace(R.id.main_fragment_container, listFragment)
.commit()
}
Transitions itself is defined like this: 转换本身的定义如下:
private fun moveTransition(): TransitionSet {
return TransitionSet().lollipop {
ordering = TransitionSet.ORDERING_TOGETHER
addTransition(ChangeBounds())
addTransition(ChangeTransform())
}
}
This code works on my Moto Z2 Play (Android close to AOSP) and on my Motorola One (Android ONE) only if: 仅在以下情况下,此代码可在我的Moto Z2 Play(Android接近AOSP)和我的Motorola One(Android ONE)上运行。
However, id does not work properly if target API is 26 or higher. 但是,如果目标API为26或更高版本,则id无法正常工作。 If target API is set to 26, shared elements are not animated .
如果将目标API设置为26,则共享元素不会设置为动画 。 Instead, both shared elements jump to their end positions while everything else is animated.
取而代之的是,两个共享元素都跳到了它们的最终位置,而其他所有元素都被动画化了。
How can I make it work without using the Support Library? 不使用支持库怎么办? Am I missing some attribute which has to be set?
我是否缺少一些必须设置的属性?
After some reasearch and thanks to the comment from Eugen Pechanec, I have concluded that using the v4 support library is the way to go . 经过一番研究,并感谢Eugen Pechanec的评论,我得出结论, 使用v4支持库是必经之路 。 Platform fragments are deprecated in API Versions 28, so we will have to mirgate sooner or later.
API版本28中不推荐使用平台片段,因此我们迟早必须进行镜像。
I cannot say that support library works flawlessly, but at least shared elements transitions are being animated. 我不能说支持库可以完美地工作,但是至少共享元素的过渡是动态的。
I was not able to make it work without the library, having invested quite a lot of time. 我花了很多时间才能在没有图书馆的情况下使之工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.