如何在ViewPager中管理otto注册方法 [英] How to manage otto registration methods in ViewPager

查看:124
本文介绍了如何在ViewPager中管理otto注册方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用otto交流服务和地图片段.在服务中,位置提供者获取发送到地图片段的位置数据.它工作完美,但我无法使用ViewPager管理otto的注册和注销方法.这个问题既不涉及otto问题也不涉及ViewPager问题.

I am using otto to communicate service and map fragment. In the service, location provider obtains location data sends to map fragment. It works perfectly but i can not manage otto's register and unregister methods with ViewPager . This question refers neither otto nor ViewPager problem.

如上所述,当服务接收到位置数据时,它将发送到地图片段.为此,我必须在其特定的生命周期事件中注册/注销片段和服务.在这一点上,我面临着ViewPager本质的问题.默认情况下,ViewPager首先加载Home和Map片段,如下所示:

As mentioned above, when the service received location data it sends to map fragment. For this, i have to register/unregister fragment and service in their specific lifecycle events. At this point i am facing a problem with nature of ViewPager . By default, ViewPager firstly loads Home and Map fragments like below

因此,

  1. 在地图片段中,无需从家刷卡到地图片段即可自动调用otto寄存器方法
  2. 在地图片段中,从底部滑动到其他位置时永远不会调用otto unregister方法

因此,此行为使我无法管理otto方法. 即使选择了家庭或设置片段,地图片段仍将处于活动状态并从服务接收位置数据.我不要这个.

So this behavior prevents me managing otto methods. Even when home or settings fragments are selected, map fragment will be alive and receiving location data from service. I do not want this.

我在GitHub和此处的ViewPager中搜索了otto的用法,但是没有任何问题或示例.在对ViewPager进行一点搜索之后,我发现有人建议使用ViewPager.setOffscreenPageLimit(0). 但这不起作用

I searched usage of otto with ViewPager on github and here, but there is no any question or example. After a bit search of ViewPager , i found some people suggests ViewPager.setOffscreenPageLimit(0). But it does not work

那么一起使用ViewPager和otto的正确方法是什么?

So whats the proper way using ViewPager and otto together?

推荐答案

如果只希望在可见的MapFragment中接收事件,则可以执行以下操作:

If you want to receive only events in your MapFragment when it is visible, then you can do something like this:

public class MapFragment extends Fragment
    @Override
    public void setMenuVisibility(final boolean visible) {
        super.setMenuVisibility(visible);

        if (visible) {
            // register here
        } else {
            //unregister here
        }
}

这篇关于如何在ViewPager中管理otto注册方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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