共享元素片段转换不起作用

时间:2018-04-12 09:14:15

标签: android android-fragments android-transitions shared-element-transition

我在一个Activity中托管了两个Fragment。他们很简单:

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_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kravtsov.transitiontest.MainActivity">

    <View
        android:id="@+id/shared_view"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_centerInParent="true"
        android:background="@android:color/holo_blue_dark" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kravtsov.transitiontest.MainActivity">

    <View
        android:id="@+id/shared_view"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_below="@+id/anchor"
        android:layout_centerHorizontal="true"
        android:background="@android:color/holo_blue_dark" />

    <TextView
        android:id="@+id/anchor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:text="WORLD"
        tools:ignore="HardcodedText" />

</RelativeLayout>

正如您所见,他们有共同的共享视图,我希望在过渡期间制作动画。问题是 - 动画永远不会出现。我可以看到进入和退出淡入淡出过渡,其余元素正常工作。只有共享元素转换不起作用。

我搜索了很多网页并模仿了很多guiedes ...我找不到答案。有人面对这样的问题吗?在哪里,我开始修复这个错误?

1 个答案:

答案 0 :(得分:0)

您的代码对我有用。我已经使用了你的布局和kotlin代码,它可以无缝地工作。我负责的一件事是导入所有类的支持包。