ListFragment和SupportMapFragment选择同步 [英] Synchronize ListFragment and SupportMapFragment selection

查看:225
本文介绍了ListFragment和SupportMapFragment选择同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个看法基本上是相同的数据:

I have 2 views of essentially the same data:

  1. <一个href="https://$c$c.google.com/p/wheelly/source/browse/src/com/wheelly/fragments/LocationsListFragment.java?name=dev"相对=nofollow>在android.support.v4.app项目列表。 ListFragment
  2. 在<一个标记href="https://$c$c.google.com/p/wheelly/source/browse/src/com/wheelly/fragments/LocationsMapFragment.java?name=dev"相对=nofollow>地图在com.google.android.gms.maps。 SupportMapFragment
  1. List of items in a android.support.v4.app.ListFragment
  2. Markers on a map in a com.google.android.gms.maps.SupportMapFragment

以上两者都使用装载机的模式,以获得相同的数据(扩展的 LoaderCallbacks ,查询的ContentProvider ,等等)

Both of the above are using loader pattern to obtain the same data (extending LoaderCallbacks, querying ContentProvider, and so on)

这两个都在一个<托管href="https://$c$c.google.com/p/wheelly/source/browse/src/com/wheelly/activity/LocationsList.java?name=dev"相对=nofollow>活动内ViewPager 。

什么将是最好的策略,为这两个片段同步当前选中的列表项/标记? (想想我的位置编辑UI,或者方向的谷歌地图与自己的左侧窗格中,并在该中心的地图)。

What will be the best strategy to synchronize currently selected list item / marker for both of these fragments? (Think of "My Places" edit UI, or "Directions" of the Google Maps with their left-hand pane and a map in the center).

情景我在想,到目前为止的:

Scenarios i'm thinking of so far:

  1. 请每一个片段手动通知它的对手有关通过回调接口选择的变化(这可能会涉及到基本活动,协调跨段通信,因为它是建议由Android的文档)。
  2. 在以某种方式使两个片段使用相同的光标,甚至ListAdapter(不管它意味着一个地图,因为现在它从光标直接填充)。
  3. (别的东西吗?)

也许有人已经处理了这个确切的情况? (我肯定会找到一些解决办法,只是想避免重新发明轮子对不起了过于概念化的问题。)

Maybe someone has already dealt with this exact case? (I'll sure find some solution, just wanted to avoid "reinventing the wheel". Sorry for a too conceptual question.)

修改(解决方案)

我觉得马切伊已经回答了我的确切问题(这样的最好的策略,和..),这样的答案都是 1 2 ; - )

I think Maciej has answered my exact question ("best strategy", and so on..), so the answers are both 1 and 2 ;-)

走进更多的细节,我的实现是这样的:

Going into more details, my implementation went like this:

起初我吓坏了与发布/订阅模式在Java中处理的巨大开销(包括界面,找到合适的地方回调,什么是不)。幸运的是,奥托总线实施吸引了我的目光,这让碎片一件微不足道的小事之间的通信。它不仅可以通知有关选择改变所有用户,也是整个装载机啪很好地适应:

At first I frightened by enormous overhead of dealing with publisher/subscriber pattern in Java (involving interfaces, finding proper places for callbacks, and what's not). Fortunately, Otto bus implementation caught my eyes, which made communication between fragments a trivial thing. Not only it is possible to notify all subscribers about selection change, but also the whole Loader Patter fit nicely:

  1. 这是奥托的样品code借BusProvider类。

  1. Borrow BusProvider class from Otto's sample code.

创建一些消息,合同履行通知数据:

Create few message contracts to carry notification data:

public class LocationSelectedEvent {  
    public long id;  
}  

public class LocationsLoadedEvent {  
    public Cursor cursor;  
}  

  • 在注释碎片接收器的方法@Subscribe(下面的例子是装载机情况下,选择改变它没有更多的复合物):

  • Annotate "receiver" methods in fragments with @Subscribe (example below is for loader case, for selection change it's no more complex):

    @Subscribe
    public void onLoadFinished(LocationsLoadedEvent event) {  
        final CursorAdapter a = (CursorAdapter) getListAdapter();  
        a.swapCursor(event.cursor);  
    }  
    

  • 请碎片听来的通知:

  • Make fragments "listening" to notifications:

    @Override  
    public void onActivityCreated(Bundle savedInstanceState) {  
        BusProvider.getInstance().register(this);  
    }  
    

  • 请片段停止听时,他们不是活着(专为片段API真正了解到硬盘的方式):

  • Make fragments to stop listening when they're not "alive" (specially true for fragments API, learned it the hard way):

    @Override  
    public void onDestroy() {  
        super.onDestroy();  
        BusProvider.getInstance().unregister(this);  
    }  
    

  • 最后,在需要触发通知(例如下面deomnstrates如何从通知的 LocationList 当光标已加载的活动):

  • Finally, trigger notifications where desired (example below deomnstrates how to notify from LocationList activity when cursor has been loaded):

    @Override
    public void onResume() {
        if(null == getLoaderManager().getLoader(0)) {
            getSupportLoaderManager().initLoader(0, null, new LoaderCallbacks<Cursor>() {
                @Override
                public Loader<Cursor> onCreateLoader(int paramInt, Bundle paramBundle) {
                    return new CursorLoader(LocationsList.this, Locations.CONTENT_URI, null, null, null, null);
                }
    
                @Override
                public void onLoadFinished(Loader<Cursor> paramLoader, Cursor cursor) {
                    BusProvider.getInstance().post(new LocationsLoadedEvent(cursor));
                }
    
                @Override
                public void onLoaderReset(Loader<Cursor> paramLoader) {
                    BusProvider.getInstance().post(new LocationsLoadedEvent(null));
                }
            });
        }
        super.onResume();
    }
    

  • 奖励:通知流程的可视化

    Bonus: notifications flow visualization

    推荐答案

    对于点击协调你1点是我会做什么,当然使用的活性和接口。

    For click-coordination your point 1 is what I would do, using Activity and interfaces of course.

    我只是很难理解为什么你想从的ContentProvider 加载相同的数据的两倍。为什么不一次在一个共享对象加载它?在应用程序的一些对象,注射单,或甚至另一个片段,该通知活动完毕,并且将数据推送到你的两个片段

    I just have hard time understanding why would you want to load the same data from ContentProvider twice. Why not load it once in a shared object? Some object inside Application, injected singleton or even another Fragment, which notifies Activity of data load complete and it pushes data to your two Fragments?

    这篇关于ListFragment和SupportMapFragment选择同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆