Android 事件总线不适用于两个事件侦听器
Android Event bus does not works with two event listener
在我的 Android 应用程序中,我使用了 Activity 和适配器用于列表视图,我的知识需要通过事件侦听器与适配器 class 和 activity 进行通信使用 EventBus,因此我创建了两个事件侦听器 classes。
我的流程是:
1) 我在activity中有一个按钮,该按钮应该与适配器class通信。
2) 如果我单击文本视图(列表视图的文本视图小部件)应该通信 Activity class。
通过以下代码,它适用于适配器与 Activity 通信,但 Activity 不与适配器 class 通信。请帮助我了解如何为 classes 进行通信?
我已经发布了完整的示例项目代码:
Activity class:
public class ListMobileActivity extends Activity {....};
private ListView list;
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(ListMobileActivity.this);
......
list.setAdapter(adapter);
// Does not communicates with Adapter.
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
EventBus.getDefault().post(new TestEvent2("test event"));
}
});
}
public void onEvent(TestFinishedEvent event) {
Log.e("TestFinishEvent ", event.test);
}
}
适配器class:
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_mobile, values);
this.context = context;
this.values = values;
EventBus.getDefault().register(this.context); // registered here.
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
.........
// its works, communicate with Activity
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
EventBus.getDefault().post(new TestFinishedEvent("ssds"));
}
});
return rowView;
}
public void onEvent(TestEvent2 event) {
Log.e("Test event 2 ", event.test);
}
}
不要每次都创建新的 EventBus
实例,使用 EventBus.getDefault()
。
添加到 类 方法 public void onEvent(Object event)
在你的MobileArrayAdapter
构造函数中
改变
EventBus.getDefault().register(this.context)
到
EventBus.getDefault().register(this)
编辑 1:
另请注意,您应始终致电 EventBus.getDefault().unregister(this);
一旦您不需要接收事件或 activity 正在发送 stopped/destroyed
您的 activity 必须注册到 EventBus。
在你的Activityclass
在 onStart() 上订阅
在 onStop()
中取消订阅
在你的适配器中
只需倾注您的活动,所有订阅者都会收到您的 post 活动。
在我的 Android 应用程序中,我使用了 Activity 和适配器用于列表视图,我的知识需要通过事件侦听器与适配器 class 和 activity 进行通信使用 EventBus,因此我创建了两个事件侦听器 classes。
我的流程是:
1) 我在activity中有一个按钮,该按钮应该与适配器class通信。
2) 如果我单击文本视图(列表视图的文本视图小部件)应该通信 Activity class。
通过以下代码,它适用于适配器与 Activity 通信,但 Activity 不与适配器 class 通信。请帮助我了解如何为 classes 进行通信?
我已经发布了完整的示例项目代码:
Activity class:
public class ListMobileActivity extends Activity {....};
private ListView list;
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(ListMobileActivity.this);
......
list.setAdapter(adapter);
// Does not communicates with Adapter.
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
EventBus.getDefault().post(new TestEvent2("test event"));
}
});
}
public void onEvent(TestFinishedEvent event) {
Log.e("TestFinishEvent ", event.test);
}
}
适配器class:
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_mobile, values);
this.context = context;
this.values = values;
EventBus.getDefault().register(this.context); // registered here.
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
.........
// its works, communicate with Activity
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
EventBus.getDefault().post(new TestFinishedEvent("ssds"));
}
});
return rowView;
}
public void onEvent(TestEvent2 event) {
Log.e("Test event 2 ", event.test);
}
}
不要每次都创建新的 EventBus
实例,使用 EventBus.getDefault()
。
添加到 类 方法 public void onEvent(Object event)
在你的MobileArrayAdapter
构造函数中
改变
EventBus.getDefault().register(this.context)
到
EventBus.getDefault().register(this)
编辑 1:
另请注意,您应始终致电 EventBus.getDefault().unregister(this);
一旦您不需要接收事件或 activity 正在发送 stopped/destroyed
您的 activity 必须注册到 EventBus。
在你的Activityclass 在 onStart() 上订阅 在 onStop()
中取消订阅在你的适配器中 只需倾注您的活动,所有订阅者都会收到您的 post 活动。