简体   繁体   English

如何将数据从对话片段发送到Viewpager中的所有片段?

[英]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.

相关问题 在片段的viewpager中,如何请求将数据从一个片段拉到另一个片段 - In a viewpager of fragments, how to request to pull data from one fragment to another 如何在相同的片段活动中将数据从片段发送到片段? - How to send data from fragment to fragment within same fragment activity? 将数据发送到viewpager的特定片段 - Send data to specific fragment of viewpager 从第二个Fragment的recyclerView中选择项目时如何切换到第一个Fragment? 两个片段都在带有 viewPager2 的 tablayout 中 - How to switch to the first fragment when selecting an item from the recyclerView of second Fragment? Both fragments are in tablayout with viewPager2 如何在viewpager中显示片段? - How to display fragment within a viewpager? 如何将数据从片段发送到片段? - How to send data from fragment to fragment? Android:将数据从由 ViewPager2 和 TabLayout 管理的片段发送到包含的活动 - Android: send data from fragments managed by a ViewPager2 and TabLayout to the containing activity 无法将数据从MainActivity传递到ViewPager片段 - Unable to pass data from MainActivity to ViewPager Fragments Viewpager中的片段只有在旁边的片段打开时才会刷新 - Fragment in Viewpager only refreshing when it's open from fragments next to it Viewpager选项卡片段和正常片段 - Viewpager Tabs Fragment & Normal Fragments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM