【问题标题】:Update one Fragment due to changes in another fragment由于另一个片段的变化而更新一个片段
【发布时间】:2014-06-08 08:26:21
【问题描述】:

情景:

我有两个片段(每个片段都在一个单独的选项卡中)做彼此相关的工作。例如:

public class A extends Fragment{
// Take user input and calculate a geo-location(this works fine)
// User can also mannually enter a location address and the geocoder
// figures out the Lat/Lng of that location.
// Uses a Asych task.
}

public class B extends Fragment{
// This a Google map. Based on Latitude & Longitude
// place a marker at that location(which is provided by class A.)
// Do other Stuff aswell 
}

问题:

我想将位置信息传递给 B 类。但是,一旦我完成了我在 A 类中的位置计算,我就无法使用标记更新地图。 我所看到的是,在我的应用程序启动的那一刻,B 类 onCreateView() 方法就会运行。这是我通常放置我的标记功能信息的地方。然而,A 类的 Asych 任务需要一些时间来计算地理位置,因此我还没有 Location 可以提供给 B 类中的标记方法。一旦 Asych 任务完成而无需再次在 A 类中调用 onCreateView() (对于 B 类),是否仍将标记添加到地图?


换句话说

我能否以某种方式传递位置信息(用于标记方法),从而在我的 Asych 任务完成后在谷歌地图上放置一个标记。

【问题讨论】:

标签: android google-maps android-fragments


【解决方案1】:

片段之间最好的通信方式是使用监听器,这里你可以找到an example how to implement that

当您的 AsyncTask 结束时,您可以调用您的侦听器,该侦听器将与您的 FragmentB 通信并在地图中添加标记

【讨论】:

    【解决方案2】:

    Fragment B 的引用传递给Fragment A 构造函数。

    然后您可以在Fragment B 中创建一个方法,将marker 添加到地图中。

    Fragment A 中下载标记位置后,您可以调用Fragment B 的引用并调用其method 来更新标记

    【讨论】:

    • 片段的构造函数错误??。片段应该没有 arg 构造函数
    • @Raghunandan,是的,你是绝对正确的 Fragment 构造函数不应该有参数。按照我的看法,没有简单的方法可以解决这个问题
    • @Raghunandan 只是说他可以使用 Fragment 的引用来获取标记,是的,有很多选项,例如回调
    • @SeahawksRdaBest 好吧,您可以选择使用接口作为活动的回调,然后与第三方库的片段 B 或 EventBus 通信。简单的选项是什么意思。首先,文档中有示例。第二次阅读 github 上的 eventbus 页面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2014-01-28
    • 2016-09-30
    • 1970-01-01
    相关资源
    最近更新 更多