簡體   English   中英

如何在使用導航組件和 safeArgs 從回收器視圖導航到片段時添加共享元素轉換?

[英]How to add shared element transition when navigating from recycler view to a fragment while using navigation components and safeArgs?

我想顯示從我的回收器視圖到片段 class 的共享元素轉換,我編寫了以下代碼:

holder.view.setOnClickListener {
            val extras = FragmentNavigatorExtras(
                holder.view.ivPersonImage to "imageView"
            )
            it.findNavController().navigate(
                HomeFragmentDirections.actionNavHomeToTransactionFragment(
                    customerId,
                    customerName,
                    customerPhoneNumber,
                    imageString
                ), null, null, extras
            )
        }

它顯示錯誤 at.navigate 為:

 None of the following functions can be called with the arguments supplied: 
private open fun navigate(@NonNull p0: NavDestination, @Nullable p1: Bundle?, @Nullable p2: NavOptions?, @Nullable p3: Navigator.Extras?): Unit defined in androidx.navigation.NavController
public open fun navigate(@IdRes p0: Int, @Nullable p1: Bundle?, @Nullable p2: NavOptions?, @Nullable p3: Navigator.Extras?): Unit defined in androidx.navigation.NavController

在片段 class 中,我正在使用:

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

似乎 safeArgs 不允許顯示轉換。 我是 Android 的新手,發現很難理解過渡和動畫。 請幫幫我,我在這里卡了這么多天了

SafeArgs允許您設置共享過渡。 只需檢查navigate() 文檔,有兩個參數重載: NavDirectionsNavigator.Extras 所以你的代碼應該是這樣的:

holder.view.setOnClickListener {
    val extras = FragmentNavigatorExtras(holder.view.ivPersonImage to "imageView")
    it.findNavController().navigate(
        HomeFragmentDirections.actionNavHomeToTransactionFragment(
            customerId,
            customerName,
            customerPhoneNumber,
            imageString
        ), 
        extras
    )
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM