Android Fragments 如何在A片段和B片段的子片段之间使用共享元素,反之亦然

mum43rcc  于 11个月前  发布在  Android
关注(0)|答案(1)|浏览(154)

我有一个splash片段,主片段有底部导航和bottom_nav_graph。我需要显示从Splash到Home片段的转换,Home片段是主片段的默认子片段(它有底部导航,Home是菜单之一)。
这些元素只在Splash Fragment和Home Fragment中使用。
飞溅碎片

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/lottie_loading"
    android:layout_width="192dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:lottie_autoPlay="true"
    android:transitionName="@string/splash_lottie"
    app:lottie_fileName="splash.json" />

个字符
主片段

// do nothing


或者我试过

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(android.R.transition.move)
    }


主页片段

<androidx.appcompat.widget.AppCompatImageView
    android:id="@+id/iv_top_logo"
    android:layout_width="110dp"
    android:layout_height="33dp"
    android:layout_marginTop="40dp"
    android:scaleType="centerCrop"
    android:scaleX="1.4"
    android:scaleY="1.4"
    android:transitionName="@string/splash_lottie"
    app:layout_constraintStart_toStartOf="@id/actv_top_label_title"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/logo_white"
    app:tint="@color/primary" />


我也试过

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(android.R.transition.move)
    }


这是LottieAnimationView到ImageView。我想不同的类型并不重要。但是transitionName很重要。
不管怎样,这不管用。我有没有漏掉什么?
而且,我需要在Home片段到UserDetail片段之间进行类似的转换,该片段来自父节点的nav_graph,与Main feragment处于同一级别。
我该如何解决这个问题?

oprakyz7

oprakyz71#

SplashFragment:
<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/lottie_loading"
    android:layout_width="192dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:lottie_autoPlay="true"
    android:transitionName="@string/splash_lottie"
    app:lottie_fileName="splash.json" />

HomeFragment:
<androidx.appcompat.widget.AppCompatImageView
    android:id="@+id/iv_top_logo"
    android:layout_width="110dp"
    android:layout_height="33dp"
    android:layout_marginTop="40dp"
    android:scaleType="centerCrop"
    android:scaleX="1.4"
    android:scaleY="1.4"
    android:transitionName="@string/splash_lottie"
    app:layout_constraintStart_toStartOf="@id/actv_top_label_title"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/logo_white"
    app:tint="@color/primary" />

SplashFragment:
val sharedElement = binding.lottieLoading
val extras = FragmentNavigatorExtras(sharedElement to sharedElement.transitionName)
findNavController().navigateSafe(R.id.action_splash_to_home, navExtras = extras)

Verify that the appropriate actions are defined in your navigation graph (nav_graph.xml) and that the IDs you use to navigate are right.

When adding or removing pieces, make sure you are using the appropriate FragmentTransaction. This should be handled for you by the navigation actions if you are using the Navigation Component.

Unless you want to change the default transition behaviour, you don't need to set sharedElementEnterTransition in HomeFragment or any other fragment.

字符串

相关问题