完整代码Gitee地址:kotlin-demo: 10天Kotlin开发计划
第七天学习内容代码:Chapter7
官方地址:
//adle -> dependencies 引入
implementation obot:eventbus:3.2.0'
EventBus 是基于观察者模式,核心是事件。通过事件的发布和订阅实现组件之间的通信,EventBus默认是一个单例存在,在Java中还需要使用Synchronized来保证线程安全。
通俗来讲,EventBus通过注册将所有订阅事件的方法储存在集合中,当有事件发布的时候,根据某些规则,匹配出符合条件的方法,调用执行,从而实现组件间的通信。
发布的事件相当于被观察者,注册的对象相当于观察者,被观察者和观察者是一对多的关系。当被观察者状态发生变化,即发布事件的时候,观察者对象将会得到通知并作出响应,即执行对应的方法。
使用Kotlin版本的EventBus,更方便的实现UI间通信,高性能、简单而强大,最主要是使用框架更为便捷;
举个栗子:
接收方必须注册与注销,在主线程订阅接收,代码如下:
override fun onCreate(savedInstanceState: Bundle?) {Create(savedInstanceState)setContentView(R.layout.activity_main)//注册,重复注册会导致崩溃Default().register(this)}override fun onDestroy() {Destroy()//注销,有注册就必须注销Default().unregister(this)}//接收消息@Subscribe(threadMode = ThreadMode.MAIN)fun onMessageEvent(event: String) {if (event == "message") {Toast.makeText(this, "收到订阅,主界面已更新", Toast.LENGTH_SHORT).show() = "①主界面更新"}}//接收消息@SuppressLint("SetTextI18n")@Subscribe(threadMode = ThreadMode.MAIN)fun onMessageEvent(event: MessageEvent) {when (pe) {MessageType.ShowLog -> {Log.e(tag, "onMessageEvent: " + String())}MessageType.ShowToast -> {Toast.makeText(this, "onMessageEvent: " + String(), Toast.LENGTH_SHORT).show() = "②" + String()}}}
发送方,代码如下:
override fun onCreate(savedInstanceState: Bundle?) {Create(savedInstanceState)setContentView(R.layout.activity_learn7)val title: TextView = findViewById(R.id.tv_ = "组件间通信"val butTip1: Button = findViewById(R.id.but_tip1)butTip1.setOnClickListener {Default().post("message")}val butTip2: Button = findViewById(R.id.but_tip2)butTip2.setOnClickListener {Default().post(MessageEvent(MessageType.ShowLog).put("打印日志"))Default().post(MessageEvent(MessageType.ShowToast).put("组件间通信"))}}
创建发送数据模型MessageEvent
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 {Int(KEY_INT)}fun getString(): String? {String(KEY_STRING)}fun getBoolean(): Boolean {Boolean(KEY_BOOL)}fun <T : Serializable> getSerializable(): Serializable {Serializable(KEY_SERIALIZABLE) as T}fun <T : Parcelable> getParcelable(): T? {Parcelable<T>(KEY_PARCELABLE)}fun getInt(key: String): Int {Int(key)}fun getString(key: String): String? {String(key)}fun getBoolean(key: String): Boolean {Boolean(key)}fun <T : Serializable> getSerializable(key: String): Serializable {Serializable(key) as T}fun <T : Parcelable> getParcelable(key: String): T? {Parcelable<T>(key)}}enum class MessageType {ShowToast,ShowLog,
}
效果展示:
greenrobot 的 EventBus 的独特之处在于它的功能:
本文发布于:2024-02-04 18:23:17,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170713566358259.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |