gpt4 book ai didi

android - 如何在运行时使用带参数的 FragmentFactory?

转载 作者:行者123 更新时间:2023-12-05 02:53:02 25 4
gpt4 key购买 nike

假设我有一个带有构造函数的 fragment ,它在运行时根据网络请求获取参数

class MyFragment(private val myArg: Int) : Fragment() {
// Do layout and other stuff
}

我做了一个示例 FragmentFactory 来传递,但我的问题是有没有更好的方法来传递参数,尤其是当我们有各种 fragment 在运行时需要参数时?

class MyFragmentFactory private constructor() : FragmentFactory() {

var myArg = 0

override fun instantiate(classLoader: ClassLoader, className: String): Fragment {

return when (className) {
MyFragment::class.java.name -> MyFragment(myArg)
else -> super.instantiate(classLoader, className)
}
}
}

有一个FragmentManager类的方法

  public final FragmentTransaction replace(@IdRes int containerViewId,
@NonNull Class<? extends Fragment> fragmentClass, @Nullable Bundle args) {
return replace(containerViewId, fragmentClass, args, null);
}

如何使用此方法,是否可以与 FragmentFactory 一起使用,以及如何使用此方法将参数传递给 fragment ?

最佳答案

不幸的是,您不能在运行时将动态参数传递给 FragmentManager
您仍然需要为此使用 bundle 和 arguments。

关于这个问题的更多细节可以追踪here并检查谷歌的response

可以找到关于它的简短博客 here

关于android - 如何在运行时使用带参数的 FragmentFactory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62393715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com