试试这个办法:将以下内容添加到您的module
水平build.gradle
文件
compile 'org.greenrobot:eventbus:3.0.0'
然后,创建一个代表你的活动常规Java类。
public class UpdateFragmentsUIEvent{
//add your values you want to pass to your other fragments
private String valueA;
private String valueB;
private String valueC;
//.....others?
public UpdateFragmentsUIEvent(){
}
//add getters and setters here
}
现在你已经创建了事件类,因为你要更新的片段(不是第一个,但你要的那些传递的数据),请执行下列操作:
public class FragmentB extends Fragment{
@Override
public void onAttach(Context context){
super.onAttach(context);
//important here - could be done inside onCreate()
EventBus.getDefault().register(this);
}
//very important here as well
public void onEvent(UpdateFragmentsUIEvent event){
/* this event instance has all your values here now */
String valueA = event.getValueA();
String valueB = event.getValueB();
/* Now you can update your views accordingly */
}
/* don't forget to unregister inside onDestroy */
@Override
public void onDestroy(){
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
//somewhere when a button is clicked after entering data
EventBus.getDefault().post(new UpdateFragmentsUIEvent(valueA, valueB, valueC));
:
最后,您输入的值实际上通知您的片段,你必须张贴这样的新崛起的情况下,我想第一个片段内部10
就这样,你就完成了!忘记接口并坚持推荐的设计模式,遵循松散耦合的代码,如本例中使用的发布者/订阅者模式。
我希望这可以帮助,请让我知道如果你需要更多的帮助!
如果您发布代码,我们可以帮助您更容易 – PsyGik