Android事件总线将数据从活动传递到选项卡片段

时间:2017-02-10 03:33:50

标签: javascript android jquery android-fragments

我的程序中有5个标签片段,我想将数据从我的工具栏按钮传递到5个标签片段,但问题是当我点击我的工具栏按钮时它只传递给3个片段,任何人都有想法解决这个问题

主要活动

这是我在MainActivity中的onclick功能

timestamp

list_fragment2

这是我在MainActivity

上点击切换按钮后收到数据的方式
    public void editpage( View view) {

    ToggleButton etMessage = (ToggleButton) findViewById(R.id.toggleButton);
    Events.ActivityFragmentMessage activityFragmentMessageEvent = new Events.ActivityFragmentMessage(String.valueOf(etMessage.getText()));
    Events.ActivityFragmentMessage2 activityFragmentMessageEvent2 = new Events.ActivityFragmentMessage2(String.valueOf(etMessage.getText()));
    Events.ActivityFragmentMessage3 activityFragmentMessageEvent3 = new Events.ActivityFragmentMessage3(String.valueOf(etMessage.getText()));
    Events.ActivityFragmentMessage4 activityFragmentMessageEvent4 = new Events.ActivityFragmentMessage4(String.valueOf(etMessage.getText()));
    Events.ActivityFragmentMessage5 activityFragmentMessageEvent5 = new Events.ActivityFragmentMessage5(String.valueOf(etMessage.getText()));
    GlobalBus.getBus().post(activityFragmentMessageEvent);
    GlobalBus.getBus().post(activityFragmentMessageEvent2);
    GlobalBus.getBus().post(activityFragmentMessageEvent3);
    GlobalBus.getBus().post(activityFragmentMessageEvent4);
    GlobalBus.getBus().post(activityFragmentMessageEvent5);

}

orderlist_fragment

@Override
public void onStart() {
    super.onStart();
    GlobalBus.getBus().register(this);
}
@Subscribe
public void getMessage(Events.ActivityFragmentMessage activityFragmentMessage) {

    messageView2 = (TextView) getView().findViewById(R.id.sum2);
    messageView2.setText(String.valueOf(activityFragmentMessage.getMessage()));
    messageView2.setText(activityFragmentMessage.getMessage());
    i = messageView2.getText().toString();
    result = Integer.parseInt(i);
    if (result == 1) {
        Nsum[0]++;
        editor.putInt(PREFS_KEY,Nsum[0]);
        editor.commit();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.detach(list_fragment2.this).attach(list_fragment2.this).commit();

    }else if (result ==0){
        editor.remove(PREFS_KEY);
        editor.commit();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.detach(list_fragment2.this).attach(list_fragment2.this).commit();
    }

}
@Override
public void onDestroyView() {
    super.onDestroyView();
    GlobalBus.getBus().unregister(this);
}

Tab4_fragment

 @Override
public void onStart() {
    super.onStart();
    GlobalBus.getBus().register(this);
}
@Subscribe
public void getMessage3(Events.ActivityFragmentMessage3 activityFragmentMessage3) {

    messageView3 = (TextView) getView().findViewById(R.id.sum3);
    messageView3.setText(String.valueOf(activityFragmentMessage3.getMessage3()));
    messageView3.setText(activityFragmentMessage3.getMessage3());
    i = messageView3.getText().toString();
    result = Integer.parseInt(i);
    if (result == 1) {
        Nsum[0]++;
        editor.putInt(PREFS_KEY,Nsum[0]);
        editor.commit();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.detach(orderlist_fragment.this).attach(orderlist_fragment.this).commit();

    }else if (result ==0){
        editor.remove(PREFS_KEY);
        editor.commit();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.detach(orderlist_fragment.this).attach(orderlist_fragment.this).commit();
    }

}
@Override
public void onDestroyView() {
    super.onDestroyView();
    GlobalBus.getBus().unregister(this);
}

1 个答案:

答案 0 :(得分:0)

这是因为尚未创建片段4和5。将 setOffscreenPageLimit(5)添加到viewPager实例中。它会将所有5个片段保留在内存中,而不是使用延迟加载机制。

官方文档链接setOffscreenPageLimit

  

设置应保留在页面两侧的页数   处于空闲状态的视图层次结构中的当前页面。页面超出此范围   在需要时,将从适配器重新创建限制。