gpt4 book ai didi

android - 如何从 Compose 中添加现有 fragment

转载 作者:行者123 更新时间:2023-12-01 23:10:57 31 4
gpt4 key购买 nike

我想从 compose 打开一个现有 fragment ,或者如果我可以全屏添加到同一个 compose 中,那也可以。还有如何通过 compose 从一个现有 fragment 导航到另一个现有 fragment 。

最佳答案

我们对在 Compose 中使用 Fragments 的建议记录在案 here .

具体来说,您应该使用 AndroidViewBinding 可组合项来扩充包含 FragmentContainerView 的 XML,该 FragmentContainerView 托管您要在 Compose 中使用的 fragment 。 AndroidViewBinding 具有特定于 fragment 的处理,这就是为什么您想在 AndroidView 上使用它。请注意,您需要为此启用 View 绑定(bind)。

示例 XML 文件:

<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:name="com.example.MyFragment" />

然后在您的 Composable 函数中:

@Composable
fun FragmentInComposeExample() {
AndroidViewBinding(MyFragmentLayoutBinding::inflate) {
val myFragment = fragmentContainerView.getFragment<MyFragment>()
// ...
}
}

关于android - 如何从 Compose 中添加现有 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69943010/

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