如何通过单击底部纸内部的按钮来替换底部纸的外部片段?
因此,我决定将我的应用程序带有单个活动。在我的一个片段(fragment(a)
)中,我打开底部表
通过单击按钮打开BS
。现在,棘手的公园,底部的底表内有一个fragmentContainerview
,其中有两个片段,我想单击此button B
内部fragmentContainerview
(在fragment(b)
)将将fragment(a)
更改为 fragment(c)和关闭底部表。
我知道,通过下面使用代码,可以交换片段和关闭底部表(在这里,我在fragmentContainerview
的外部单击按钮,但仍在底部表中):
ButtonSH.setOnClickListner{
parentFragmentManager.commit {
// replace fragment
replace<FragmentC>(R.id.fragment_container_that_holds_fragmets_A_and_C)
}
// close bottom sheet
dismiss()
}
但是我只是不明白如何了解通过单击按钮b
在fragmentContainerview
in fragment(b)
中进行同样的操作。
如果重要的话,fragment(a)
不直接在活动中,它在其他一些片段中。
这是一个可以更好地理解的图像:
So I've decided to make my app with single Activity. In one of my fragments (fragment(A)
) I open bottom sheet
by clicking Button open BS
. Now the tricky park, inside bottom sheet there is a FragmentContainerView
that has two fragments inside and I want to click Button B
inside this FragmentContainerView
(in fragment(B)
) that would change fragment(A)
to fragment(C)
and close bottom sheet.
I'm aware that by using code below it's possible to swap fragment and close bottom sheet (here I'm clicking button outside of FragmentContainerView
but still inside bottom sheet):
ButtonSH.setOnClickListner{
parentFragmentManager.commit {
// replace fragment
replace<FragmentC>(R.id.fragment_container_that_holds_fragmets_A_and_C)
}
// close bottom sheet
dismiss()
}
But I just can't understand how to do the same by clicking Button B
inside FragmentContainerView
in fragment(b)
.
If it matters, fragment(A)
is not directly inside activity, it is inside a few other fragments.
Here is an image for better understanding:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,我的解决方案是为底部纸及其片段制作共享视图模型,并为容器中使用另一个共享视图模型,该容器容纳片段(a)和底部纸。我观察到ViewModels内部的变量以替换片段。有关正式文档,请访问 this
So my solution was to make sharedViewModel for bottom sheet and its fragments, and to use another sharedViewModel for container that holds fragment(A) and for bottom sheet. I observe variables inside viewModels to replace fragments. For official documentation visit this