共享元素片段转换不起作用
Posted
技术标签:
【中文标题】共享元素片段转换不起作用【英文标题】:Shared element fragment transition not working 【发布时间】:2018-09-22 08:56:57 【问题描述】:我在一个活动中托管了两个片段。它们非常简单:
class OneFragment : Fragment()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View
val view = inflater.inflate(R.layout.fragment_one, container, false)
val sharedView = view.findViewById<View>(R.id.shared_view)
ViewCompat.setTransitionName(sharedView, "test")
view.findViewById<Button>(R.id.go_button).setOnClickListener
val fragmentTransaction = fragmentManager.beginTransaction()
val oneFragment = fragmentManager.findFragmentById(R.id.fragment_container_frame_layout)
val twoFragment = TwoFragment()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
val moveTransition = TransitionInflater.from(activity).inflateTransition(android.R.transition.move)
oneFragment.exitTransition = Fade()
val transitionSet = TransitionSet()
transitionSet.addTransition(TransitionInflater.from(activity).inflateTransition(android.R.transition.move))
transitionSet.duration = 600
twoFragment.sharedElementEnterTransition = transitionSet
twoFragment.enterTransition = Fade()
fragmentTransaction.addToBackStack(TwoFragment::class.java.simpleName)
fragmentTransaction.addSharedElement(sharedView, "test")
fragmentTransaction.replace(R.id.fragment_container_frame_layout, twoFragment)
fragmentTransaction.commit()
else
fragmentTransaction.addToBackStack(TwoFragment::class.java.simpleName)
fragmentTransaction.replace(R.id.fragment_container_frame_layout, twoFragment)
fragmentTransaction.commit()
return view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context="com.kravtsov.transitiontest.MainActivity">
<View
android:id="@+id/shared_view"
android:layout_
android:layout_
android:layout_centerInParent="true"
android:background="@android:color/holo_blue_dark" />
<TextView
android:layout_
android:layout_
android:layout_below="@id/shared_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="HELLO"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/go_button"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="GO"
tools:ignore="HardcodedText" />
</RelativeLayout>
第二个:
class TwoFragment : Fragment()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View
val view = inflater.inflate(R.layout.fragment_two, container, false)
val sharedView = view.findViewById<View>(R.id.shared_view)
ViewCompat.setTransitionName(sharedView, "test")
return view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context="com.kravtsov.transitiontest.MainActivity">
<View
android:id="@+id/shared_view"
android:layout_
android:layout_
android:layout_below="@+id/anchor"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_blue_dark" />
<TextView
android:id="@+id/anchor"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="WORLD"
tools:ignore="HardcodedText" />
</RelativeLayout>
如您所见,它们有共同的共享视图,我想在过渡期间制作动画。问题是 - 动画永远不会出现。我可以看到其余元素正常工作的进入和退出淡入淡出过渡。只有共享元素转换不起作用。
我在网上搜索了很多并模仿了很多指南...我仍然没有找到答案。有没有人面临这样的问题?我应该从哪里开始修复这个错误?
【问题讨论】:
尝试申请transaction.setReorderingAllowed(true)
。
对 transaction.setReorderingAllowed(true) 无效
【参考方案1】:
您的代码对我有用。我已经使用了您的布局和 kotlin 代码,并且可以无缝运行。我负责的一件事是导入所有类别的支持包。
【讨论】:
以上是关于共享元素片段转换不起作用的主要内容,如果未能解决你的问题,请参考以下文章