小编Net*_*hak的帖子

第一次打开fragment时ComposeView非常慢

我将 compose 添加到现有项目中。我用compose重写了一个fragment ui,当我启动该fragment时,需要很长时间才能启动。

从另一个片段添加一个片段:

 val fragment = FragmentWithComposeUi()
 requireActivity().addFragment(fragment, R.id.fragment_container, "FragmentWithComposeUi")
Run Code Online (Sandbox Code Playgroud)

addFragment()添加片段的功能。

fun FragmentActivity.addFragment(fragment: Fragment, container: Int, tag:String) {
    val currentFragment = supportFragmentManager.findFragmentByTag(tag)
    if (currentFragment == null) {
        supportFragmentManager.beginTransaction()
            .setReorderingAllowed(true)
            .add(container, fragment, tag)
            .addToBackStack(tag)
            .commit()
    }
}
Run Code Online (Sandbox Code Playgroud)

FragmentWithComposeUi 类:

class FragmentWithComposeUi: Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply{
            setContent {
               //some ui
            }
        }
    }

Run Code Online (Sandbox Code Playgroud)

构建.gradle

我也尝试使用新的 compose 版本 1.3.0-alpha01,但没有帮助。

buildscript {
    ext {
        compose_version = '1.1.1'
        compose_compiler_version = …
Run Code Online (Sandbox Code Playgroud)

android android-fragments kotlin android-jetpack-compose

11
推荐指数
0
解决办法
602
查看次数