当我从FragmentA触发事件时,我在MyComctivity的两个片段的组合(按钮点击)我想在FragmentB按钮状态中更改为启用true和textview setText("新文本") FragmentB,当我运行我的应用程序输出时显示没有错误但不做任何更改 这是我的片段,它发射事件
Fragment A
...//some code
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_a, container, false);
...//more code inside onCreateView
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//HERE I POST THE EVENT
EventBus.getDefault().post(new ButtonEvent(true));
// HERE I PUT SOUT TO SHOW IF EVENT IS FIRED
System.out.println("YOU FIRED THE EVENT");// THIS MSG IS SHOWN CORRECTLY
}
});
}
@Subscribe
public void onEvent(ButtonEvent event){
}
@Override
public void onStart() {
super.onStart();
if (!EventBus.getDefault().isRegistered(getActivity())) {
EventBus.getDefault().register(this);
}
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
这里是FragmentB
FragmentB extends Fragment{
...//some code
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_b, container, false);
btnNuevoMed= (Button) rootView.findViewById(R.id.btnNuevoMed);
btnNuevoMed.setEnabled(false);
txtMed= (TextView)rootView.findViewById(R.id.txtMed);
//...some other code
}
//HERE IS MI SUBSCRIBER
@Subscribe
public void onEvent(ButtonEvent event){
// HERE I PUT A SOUT TO SHOW IF REACH THE METHOD
System.out.println("YOU ARE HEREEEEE"); //BUT NEVER REACH THIS METHOD . WHY?
btnNuevoMed.setEnabled(event.status);
btnNuevoMed.setText("hELLOOO");
txtMed.setText("Modification Success");
}
@Override
public void onStart() {
super.onStart();
if (!EventBus.getDefault().isRegistered(getActivity())) {
EventBus.getDefault().register(this);
}
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
这是我的按钮xml
<TextView
android:id="@+id/txtMed"
android:text="MEDICACION"
style="@style/textoTitulos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
<Button
android:text="Nuevo Boton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnNuevoMed"
/>
即时通讯使用eventbus 3.0.0。我的代码出了什么问题?我怎么能解决这个问题?为什么我无法达到听众方法?提前谢谢..
答案 0 :(得分:0)
试试这个:
在片段中像这样的帖子事件
EventBus.getDefault().postSticky(new ButtonEvent(true));
在Fragment B寄存器中订阅如下:
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onEvent(ButtonEvent event){
//