安卓系统中使用eventbus3.0实现片段通信

  介绍

今天就跟大家聊聊有关Android中使用eventbus3.0实现片段通信,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

<强> 1。概述

在之前的博文中简单介绍过如何实现片段之间的信息交互:《Android中片段与活动之间的交互(两种实现方式)》,今天继续给大家介绍一种可以实现此效果的另外一种方式EventBus。(相比于处理程序,接口回调,包传参,这个简单好用到哭)

<强> EventBus是Android下高效的发布/订阅事件的消息总线。作用是可以代替传统的意图,处理程序,广播或接口函数在片段,活动,服务,线程之间传递数据进行通信,执行方法。做为消息总线,有三个主要元素:

(1)事件:事件

(2)用户:事件订阅者,接受特定的事件

(3)出版者:事件发布者,用于通知用户有事件发生

结合EventBus以上的三个元素,我们也可以称其为一种观察者设计模式。

EventBus官网链接http://greenrobot.org/eventbus/

EventBus GitHub链接https://github.com/greenrobot/EventBus

前期相关博文链接:

Android中片段与活动之间的交互(两种实现方式)

Android中片段的两种创建方式

<强> 2.演示示例

(1)示例中左侧的按钮,潘侯爷与碧空海触发的事件为EventBus的普通事件发布

(2)左侧粘性事件按钮发布的为粘性事件

 Android中使用eventbus3.0实现片段通信

<强> 3。实现步骤

本次演示架构:

,  Android中使用eventbus3.0实现片段通信

使用AndroidStudio2.2。仍然采用在构建。gradle下中依赖关系下直接添加如下代码:

<代码>编译& # 39;org.greenrobot: eventbus: 3.0.0 # 39;

同步后完成依赖添加。

& lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比;   http://schemas.android.com/apk/res/android" & lt; LinearLayout xmlns: android=?;   xmlns:工具=癶ttp://schemas.android.com/tools"   android: id=癅 + id/activity_main"   android: layout_width=癿atch_parent"   android: layout_height=癿atch_parent"   android:取向=癶orizontal"   工具:上下文=癱om.mly.panhouye.eventbustest.MainActivity"比;   LinearLayout & lt;   android: layout_width=? dp"   android: layout_height=癿atch_parent"   android: layout_weight=?”;   android:取向=皏ertical"   android:背景=? 6 f6669"比;   & lt;按钮   android: layout_gravity=癱enter_horizontal"   android: id=癅 + id/panhouye"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android:text="ŋ"/比;   & lt;按钮   android: layout_gravity=癱enter_horizontal"   android: id=癅 + id/bikonghai"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android:文本=?# 831;及# 1402;“/比;   & lt;按钮   android: layout_gravity=癱enter_horizontal"   android: id=癅 + id/postSticky"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android:文本=?# 1395;Д& # 642;“/比;   & lt;/LinearLayout>   & lt; FrameLayout   android: id=癅 + id/framelayout"   android: layout_width=? dp"   android: layout_height=癿atch_parent"   android: layout_weight=?“祝辞& lt;/FrameLayout>   & lt;/LinearLayout>

& lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比;   http://schemas.android.com/apk/res/android" & lt; LinearLayout xmlns: android=?;   android:取向=皏ertical"android: layout_width=癿atch_parent"   android: layout_height=癿atch_parent"比;   & lt; TextView   android: id=癅 + id/tv"   android: layout_width=癿atch_parent"   android: layout_height=癿atch_parent"   android:文本=懊挥衐ata"   android: textSize=?0 sp"   android:重力=癱enter_horizontal"/比;   & lt;/LinearLayout>

& lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比;   http://schemas.android.com/apk/res/android" & lt; RelativeLayout xmlns: android=?;   xmlns:工具=癶ttp://schemas.android.com/tools"   android: id=癅 + id/activity_main2"   android: layout_width=癿atch_parent"   android: layout_height=癿atch_parent"   工具:上下文=癱om.mly.panhouye.eventbustest.Main2Activity"比;   & lt; TextView   android: layout_width=癿atch_parent"   android: layout_height=癿atch_parent"   android: textSize=?0 sp"   android:重力=癱enter_horizontal"   android: id=癅 + id/tv"   android:文本=懊挥衐ata"/比;   & lt;/RelativeLayout>

安卓系统中使用eventbus3.0实现片段通信