2

我想显示从我的回收器视图到片段类的共享元素转换,我编写了以下代码:

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

它在 .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

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

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

似乎 safeArgs 不允许显示转换。我是 Android 新手,很难理解过渡和动画。请帮帮我,我在这里卡了这么多天了

4

1 回答 1

1

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
    )
}
于 2021-04-01T19:44:15.477 回答