简体   繁体   English

如何将一个片段与另一个片段连接

[英]How to Connect with one fragment with another fragment

I am new with Fragment.我是 Fragment 的新手。 My problem is how to do it, when I selected my (OneFragment.java) radio button is Selected then another (Threefragment.java) will also be selected and show the data.我的问题是如何做到这一点,当我选择我的 (OneFragment.java) 单选按钮时,另一个 (Threefragment.java) 也将被选中并显示数据。

Here some Images Of My Product, I hope anyone can help me out.这是我的产品的一些图片,我希望任何人都可以帮助我。

This is My FirstFragment.Java这是我的第一个Fragment.Java

This is My ThirdFragment.Java这是我的第三个片段.Java

There are many ways to communicate between fragments.片段之间有多种通信方式。 The best way is to use fragment managers.最好的方法是使用片段管理器。

In fragment 3, initialize the listener.在片段 3 中,初始化侦听器。

setFragmentResultListener("requestKey") { key, bundle ->
    // Any type can be passed via to the bundle
    val result = bundle.getString("data")
    // Do something with the result...
}

From fragment 1, you can pass the data with the help of request keys and bundle.从片段 1 开始,您可以在请求键和包的帮助下传递数据。

setFragmentResult("requestKey", Bundle())

Make sure the fragment version is up to date.确保片段版本是最新的。

You can use shared ViewModel to passing the data between fragments using observables to retain the values and shows in any fragment through shared ViewModel.您可以使用共享ViewModel在片段之间传递数据,使用 observables 保留值并通过共享 ViewModel 在任何片段中显示。 Here is proper Implementation of shared ViewModel这是共享ViewModel正确实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM