首页 >> 大全

Kotlin开发第七天,EventBus组件间通信

2023-12-05 大全 19 作者:考证青年

完整代码Gitee地址:-demo: 10天开发计划

第七天学习内容代码:

-

官方地址:

//build.gradle -> dependencies 引入
implementation 'org.greenrobot:eventbus:3.2.0'

原理

是基于观察者模式,核心是事件。通过事件的发布和订阅实现组件之间的通信,默认是一个单例存在,在Java中还需要使用来保证线程安全。

通俗来讲,通过注册将所有订阅事件的方法储存在集合中,当有事件发布的时候,根据某些规则,匹配出符合条件的方法,调用执行,从而实现组件间的通信。

发布的事件相当于被观察者,注册的对象相当于观察者,被观察者和观察者是一对多的关系。当被观察者状态发生变化,即发布事件的时候,观察者对象将会得到通知并作出响应,即执行对应的方法。

使用

使用版本的,更方便的实现UI间通信,高性能、简单而强大,最主要是使用框架更为便捷;

举个栗子:

接收方必须注册与注销,在主线程订阅接收,代码如下:

    override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//注册,重复注册会导致崩溃EventBus.getDefault().register(this)}override fun onDestroy() {super.onDestroy()//注销,有注册就必须注销EventBus.getDefault().unregister(this)}//接收消息@Subscribe(threadMode = ThreadMode.MAIN)fun onMessageEvent(event: String) {if (event == "message") {Toast.makeText(this, "收到订阅,主界面已更新", Toast.LENGTH_SHORT).show()button7.text = "①主界面更新"}}//接收消息@SuppressLint("SetTextI18n")@Subscribe(threadMode = ThreadMode.MAIN)fun onMessageEvent(event: MessageEvent) {when (event.type) {MessageType.ShowLog -> {Log.e(tag, "onMessageEvent: " + event.getString())}MessageType.ShowToast -> {Toast.makeText(this, "onMessageEvent: " + event.getString(), Toast.LENGTH_SHORT).show()button7.text = "②" + event.getString()}}}

发送方,代码如下:

    override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_learn7)val title: TextView = findViewById(R.id.tv_title)title.text = "组件间通信"val butTip1: Button = findViewById(R.id.but_tip1)butTip1.setOnClickListener {EventBus.getDefault().post("message")}val butTip2: Button = findViewById(R.id.but_tip2)butTip2.setOnClickListener {EventBus.getDefault().post(MessageEvent(MessageType.ShowLog).put("打印日志"))EventBus.getDefault().post(MessageEvent(MessageType.ShowToast).put("组件间通信"))}}

创建发送数据模型

private const val KEY_INT = "key_int"
private const val KEY_STRING = "key_string"
private const val KEY_BOOL = "key_bool"
private const val KEY_SERIALIZABLE = "key_serializable"
private const val KEY_PARCELABLE = "key_parcelable"data class MessageEvent(var type: MessageType) {var bundle = Bundle()fun put(value: Int): MessageEvent {bundle.putInt(KEY_INT, value)return this}fun put(value: String): MessageEvent {bundle.putString(KEY_STRING, value)return this}fun put(value: Boolean): MessageEvent {bundle.putBoolean(KEY_BOOL, value)return this}fun put(value: Serializable): MessageEvent {bundle.putSerializable(KEY_SERIALIZABLE, value)return this}fun put(value: Parcelable): MessageEvent {bundle.putParcelable(KEY_PARCELABLE, value)return this}fun put(key: String, value: Int): MessageEvent {bundle.putInt(key, value)return this}fun put(key: String, value: String): MessageEvent {bundle.putString(key, value)return this}fun put(key: String, value: Boolean): MessageEvent {bundle.putBoolean(key, value)return this}fun put(key: String, value: Serializable): MessageEvent {bundle.putSerializable(key, value)return this}fun put(key: String, value: Parcelable): MessageEvent {bundle.putParcelable(key, value)return this}//===============================================================fun getInt(): Int {return bundle.getInt(KEY_INT)}fun getString(): String? {return bundle.getString(KEY_STRING)}fun getBoolean(): Boolean {return bundle.getBoolean(KEY_BOOL)}fun  getSerializable(): Serializable {return bundle.getSerializable(KEY_SERIALIZABLE) as T}fun  getParcelable(): T? {return bundle.getParcelable(KEY_PARCELABLE)}fun getInt(key: String): Int {return bundle.getInt(key)}fun getString(key: String): String? {return bundle.getString(key)}fun getBoolean(key: String): Boolean {return bundle.getBoolean(key)}fun  getSerializable(key: String): Serializable {return bundle.getSerializable(key) as T}fun  getParcelable(key: String): T? {return bundle.getParcelable(key)}}enum class MessageType {ShowToast,ShowLog,
}

效果展示:

事件总线功能

的 的独特之处在于它的功能:

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了