片段到片段共享元素无法使用androidx.fragment.app.Fragment

时间:2019-01-27 07:29:15

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

我尝试实现片段之间的共享元素转换,但是它不起作用。我在片段中使用了androidx.fragment.app.Fragment。

片段A:

xml:

 <TextView
            android:id="@+id/btnGetStarted"
            style="@style/buttonTextStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_transparent_curved"
            android:drawablePadding="@dimen/standard_16"
            android:gravity="center"
            android:onClick="@{(v)->iView.onClick(v)}"
            android:text="@string/get_started"
            android:textAllCaps="true"
            android:transitionName="@string/transition_splash"
            app:layout_constraintBottom_toTopOf="@+id/guide_H95"
            app:layout_constraintEnd_toEndOf="@id/guide_V80"
            app:layout_constraintStart_toStartOf="@id/guide_V20"
            app:layout_constraintTop_toTopOf="@+id/guide_H70"
            app:layout_constraintVertical_bias="1" />

代码:

fun replaceFragment(layoutId: Int, fragment: Fragment, view: View? = null) {
    val fragmentTransaction = supportFragmentManager.beginTransaction()
    if (view != null)
        fragmentTransaction.addSharedElement(view, ViewCompat.getTransitionName(view)!!).setReorderingAllowed(true)
    fragmentTransaction.replace(layoutId, fragment).commit()
}

片段B:

<TextView
                android:id="@+id/btnExplore"
                style="@style/buttonTextStyle"
                android:transitionName="@string/transition_splash"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:background="@drawable/bg_transparent_curved"
                android:drawablePadding="@dimen/standard_16"
                android:gravity="center"
                android:onClick="@{(v)->iView.onClick(v)}"
                android:text="@string/explore_ray"
                android:textSize="@dimen/rounded_button_text_size"
                app:layout_constraintBottom_toTopOf="@+id/txtOr"
                app:layout_constraintEnd_toEndOf="@id/guide_V80"
                app:layout_constraintStart_toStartOf="@id/guide_V20"
                app:layout_constraintTop_toTopOf="@+id/guide_H70" />

0 个答案:

没有答案