Android Kotlin版封装EventBus

文章目录

Android Kotlin版封装EventBus

代码封装

添加依赖库

复制代码
implementation("org.greenrobot:eventbus:3.3.1")

定义消息类

kotlin 复制代码
class MessageEvent<T> {
    var code: Int
    var data: T? = null

    constructor(code: Int) {
        this.code = code
    }

    constructor(code: Int, data: T) {
        this.code = code
        this.data = data
    }
}

定义常量值

kotlin 复制代码
object MessageEventCode {
    const val REFRESH = 0xB001 //刷新
    const val DELETE = 0xB002 //删除
    const val ADD = 0xB003 //增加
}

定义注解

kotlin 复制代码
@Target(AnnotationTarget.TYPE, AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class BindEventBus

定义工具类

kotlin 复制代码
object EventBusUtils {
    /**
     * 注册
     */
    fun register(subscriber: Any) {
        EventBus.getDefault().register(subscriber)
    }

    /**
     * 取消注册
     */
    fun unregister(subscriber: Any) {
        EventBus.getDefault().unregister(subscriber)
    }

    /**
     * 发送普通事件
     */
    fun post(event: MessageEvent<*>) {
        EventBus.getDefault().post(event)
    }

    /**
     * 发送粘性事件
     */
    fun postSticky(event: MessageEvent<*>) {
        EventBus.getDefault().postSticky(event)
    }

    /**
     * 判断是否注册Eventbus
     */
    fun isRegister(clz: Any): Boolean {
        return clz.javaClass.isAnnotationPresent(BindEventBus::class.java)
    }
}

使用

在Activity中

kotlin 复制代码
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_event_bus)
    if (EventBusUtils.isRegister(this)) {
        EventBusUtils.register(this)
    }
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: MessageEvent<*>) {
    when (event.code) {
        MessageEventCode.REFRESH -> {
            tvMessage.text = "刷新数据"
        }
        MessageEventCode.DELETE -> {
            tvMessage.text = "删除数据"
        }
        MessageEventCode.ADD -> {
            tvMessage.text = "添加数据:${event.data}"
        }
    }
}

override fun onDestroy() {
    super.onDestroy()
    if (EventBusUtils.isRegister(this)) {
        EventBusUtils.unregister(this)
    }
}

在Fragment中

kotlin 复制代码
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    if (EventBusUtils.isRegister(this)) {
        EventBusUtils.register(this)
    }
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: MessageEvent<*>) {
    when (event.code) {
        MessageEventCode.REFRESH -> {
            tvMessage.text = "刷新数据"
        }
        MessageEventCode.DELETE -> {
            tvMessage.text = "删除数据"
        }
        MessageEventCode.ADD -> {
            tvMessage.text = "添加数据:${event.data}"
        }
    }
}

override fun onDestroyView() {
    super.onDestroyView()
    if (EventBusUtils.isRegister(this)) {
        EventBusUtils.unregister(this)
    }
}

发送事件

kotlin 复制代码
EventBusUtils.post(MessageEvent<Nothing>(MessageEventCode.REFRESH))
kotlin 复制代码
EventBusUtils.post(MessageEvent<Nothing>(MessageEventCode.DELETE))
kotlin 复制代码
EventBusUtils.post(MessageEvent(MessageEventCode.ADD, "hello world"))

源码下载

相关推荐
shaominjin1239 分钟前
Android 约束布局(ConstraintLayout)的权重机制:用法与对比解析
android·网络
我命由我123451 小时前
Android 对话框 - 对话框全屏显示(设置 Window 属性、使用自定义样式、继承 DialogFragment 实现、继承 Dialog 实现)
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
怪兽20142 小时前
请例举 Android 中常用布局类型,并简述其用法以及排版效率
android·面试
应用市场2 小时前
Android Bootloader启动逻辑深度解析
android
江太翁2 小时前
Kotlin 与 Java 互操作中常用注解
java·python·kotlin
爱吃水蜜桃的奥特曼3 小时前
玩Android Harmony next版,通过项目了解harmony项目快速搭建开发
android·harmonyos
shaominjin1233 小时前
Android 中 RecyclerView 与 ListView 的深度对比:从设计到实践
android
vocal3 小时前
【我的AOSP第一课】AOSP 下载、编译与运行
android
Lei活在当下4 小时前
【业务场景架构实战】8. 订单状态流转在 UI 端的呈现设计
android·设计模式·架构
小趴菜82274 小时前
Android中加载unity aar包实现方案
android·unity·游戏引擎