作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从 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/
我是一名优秀的程序员,十分优秀!