[英]How to send data from a dialogue fragment to all fragments within a Viewpager?
I have run into the problem of sending data, a simple int type, from a dialogue fragment to all the fragments within a viewpager. 我遇到了从对话片段向viewpager中的所有片段发送数据(一种简单的int类型)的问题。 I have an activity that hosts both the dialogue fragment and the viewpager. 我有一个同时主持对话片段和Viewpager的活动。 I know how to send data from the dialogue fragment to the activity using an interface defined in the dialogue fragment and implementing it within the activity itself. 我知道如何使用对话片段中定义的接口将数据从对话片段发送到活动,并在活动本身中实现该接口。 I was thinking maybe it had something to do with the onAttach method but i am not sure. 我当时在想也许与onAttach方法有关,但我不确定。 i feel i am overlooking a simple solution here. 我觉得我在这里忽略了一个简单的解决方案。 please help! 请帮忙! thanks! 谢谢!
You can create a shared View Model containing a LiveData Object(holding your int). 您可以创建一个包含LiveData对象(持有您的int)的共享视图模型。 When you want to send data from the dialog fragment to the fragments in your view pager you can call the setValue method on the live data object and set up observers in each view pager fragment that are notified when the LiveData Object changes. 当您要将数据从对话框片段发送到视图分页器中的片段时,可以在实时数据对象上调用setValue方法,并在每个视图分页器片段中设置观察者,当LiveData对象发生更改时会通知这些观察者。 If this is new to you then I recommend reading the Android Architecture components documentation https://developer.android.com/topic/libraries/architecture/viewmodel https://developer.android.com/topic/libraries/architecture/livedata I hope that this helps :) 如果对您而言这是新手,那么我建议您阅读Android体系结构组件文档https://developer.android.com/topic/libraries/architecture/viewmodel https://developer.android.com/topic/libraries/architecture/livedata我希望这会有所帮助:)
Learn " setTargetFragment()
" 学习“ setTargetFragment()
”
Where " startActivityForResult()
" establishes a relationship between 2 activities, " setTargetFragment()
" defines the caller/called relationship between 2 fragments. 其中“ startActivityForResult()
”在2个活动之间建立关系,“ setTargetFragment()
”定义2个片段之间的调用者/被叫关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.