EventBus是一款针对Android优化的发布/订阅事件总线,简化组件(线程)之间及通信, 避免了复杂且容易出错的依赖关系和生命周期问题。其优点是开销小,代码更优雅,以及将发送者和接收者解耦。
/* Additional fields if needed */
}
EventBus.getDefault().register(this);
此时,应使用注解@Subscribe - 订阅消息事件,接收事件有四种模式等会介绍
发布消息
使用实例

在MainActivity打开,点击按钮"Start the activit of news"进入第二个Activity.第二个Activity由两个Fragment组成,左边的Fragment(FrmA)是一个含有
ListView的消息列表,右边的Fragment(FrmB)只有一个TextView用来显示ListView item点击所对应的信息,同时MainActivity显示相应的信息。
具体代码框架
XML
1.1 aty_main.xml
1.2 aty_news.xml
1.3 frm_detail.xml
1.4 frm_news_list.xml
2.自定义类
3. 注册EventBus
在Gif中可以看到,MainActivity和FrmB中用来接收消息,并对消息做出相应处理,此时我们应在MainActivity和
FrmB中对EventBus进行注册和注销操作
3.1 MainActivity
3.2 FrmB
4. 发布消息
在单击FrmA中ListView的item时进行事件分发,并传递相应的消息NewsEvent。
到此,EventBus的简单使用已完成。后续,深入学习EventBus的ThreadMode、粘性事件等。
参考资料:
1. 官方文档
2. EventBus 3.0的用法详解
3. Android消息传递之EventBus 3.0使用详解
添加依赖库(最新版本为3.0.0)
compile 'org.greenrobot:eventbus:3.0.0'
四部曲
声明事件,其实就是定义一个类,用来传递消息
public class MessageEvent {/* Additional fields if needed */
}
在接收界面注册EventBus,可在多处注册
EventBus.getDefault().register(this);
此时,应使用注解@Subscribe - 订阅消息事件,接收事件有四种模式等会介绍
@Subscribe
public void onEvent(MessageEvent event) {
/* Do something */
};
注:注册EventBus后,若不注解订阅消息,会报异常
org.greenrobot.eventbus.EventBusException: Subscriber class *** and its super classes have no public methods with the @Subscribe annotation
发布消息
EventBus.getDefault().post(event);
注销EventBus
EventBus.getDefault().unregister(this);
使用实例
老惯例,先看Gif效果图。
在MainActivity打开,点击按钮"Start the activit of news"进入第二个Activity.第二个Activity由两个Fragment组成,左边的Fragment(FrmA)是一个含有
ListView的消息列表,右边的Fragment(FrmB)只有一个TextView用来显示ListView item点击所对应的信息,同时MainActivity显示相应的信息。
具体代码框架
XML
1.1 aty_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".AtyMain">
<Button
android:id="@+id/btn_news"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Start the activit of news"/>
<TextView
android:id="@+id/tv_news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:textSize="18sp"/>
</LinearLayout>
1.2 aty_news.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="teaphy.evenbusdemo.aty.AtyNews">
<fragment
android:id="@+id/frm_newsList"
android:name="teaphy.evenbusdemo.frm.FrmNewsList"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/frm_news_list" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/colorPrimary"/>
<fragment
android:id="@+id/frm_detail"
android:name="teaphy.evenbusdemo.frm.FrmDetail"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/frm_detail" />
</LinearLayout>
1.3 frm_detail.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="teaphy.evenbusdemo.frm.FrmDetail">
<TextView
android:id="@+id/tv_newsDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment" />
</LinearLayout>
1.4 frm_news_list.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="teaphy.evenbusdemo.frm.FrmNewsList">
<ListView
android:id="@+id/lv_news"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</FrameLayout>
2.自定义类
public class NewsEvent {
int pos;
String news;
public NewsEvent(int pos, String news) {
this.pos = pos;
this.news = news;
}
******
}
3. 注册EventBus
在Gif中可以看到,MainActivity和FrmB中用来接收消息,并对消息做出相应处理,此时我们应在MainActivity和
FrmB中对EventBus进行注册和注销操作
3.1 MainActivity
public class AtyMain extends AppCompatActivity {
****
@Subscribe(threadMode = ThreadMode.MAIN)
public void showNews(NewsEvent event) {
tvNews.setText("postiosn = " + event.getPos() + " | news: " + event.getNews());
}
}
3.2 FrmB
public class FrmDetail extends Fragment {
***
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 注册EventBus
EventBus.getDefault().register(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.frm_detail, container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void onDestroy() {
super.onDestroy();
// 注销EventBus
EventBus.getDefault().unregister(this);
}
// 订阅事件处理
@Subscribe(threadMode = ThreadMode.MAIN)
public void showNews(NewsEvent event) {
tvNewsDetail.setText("postiosn = " + event.getPos() + " | news: " + event.getNews());
Log.i("123", "TAG: " + TAG + " | MAIN |" + event.toString());
}
}
4. 发布消息
在单击FrmA中ListView的item时进行事件分发,并传递相应的消息NewsEvent。
public class FrmNewsList extends Fragment {
*******
@OnItemClick(R.id.lv_news)
public void onItemClickForNews(int position) {
NewsEvent event = new NewsEvent(position, mAdapter.getItem(position).toString());
Log.i("123", "TAG: " + TAG + " " + event.toString());
// 发送消息
EventBus.getDefault().post(new NewsEvent(position, event.toString()));
}
}
到此,EventBus的简单使用已完成。后续,深入学习EventBus的ThreadMode、粘性事件等。
参考资料:
1. 官方文档
2. EventBus 3.0的用法详解
3. Android消息传递之EventBus 3.0使用详解