![](/img/trans.png)
[英]How to change fragment from the Recycler View OnClick Listener when RecyclerView item is clicked using Navigation Components
[英]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()
文檔,有兩個參數重載: NavDirections
和Navigator.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.