0

我有一个带有四个选项卡的选项卡式活动。每个选项卡都有自己独特的片段。我想从前三个选项卡中收集数据,对其进行分析,然后将分析后的数据发送到第四个选项卡中的 TextView。我该怎么做?

4

1 回答 1

1

我能想到的解决方案有两种,

首先,保留活动中所有 4 个选项卡的参考。从您的片段中,您可以访问活动getActivity()并调用相关的更新函数。

public class YourActivity {
   ...
   public void updateFourthFragment(Object data) {
       fourthFragment.update();
   }
}

public class FirstFragment {
   ...
   public void onInput() {
       ((YourActivity) getActivity()).updateFourthFragment(input);
   }
}
...

其次,使用事件总线,例如http://square.github.io/otto/

public class FirstFragment {
    ...
    public void onInput() {
        bus.post(new InputEvent(input));
    }
}

public class FourthFragment {
    ...
    @Subscribe
    public void onInputEvent(InputEvent event) {
        handleInput(event.getInput());
    }
}
于 2016-06-17T02:41:56.577 回答