本文主要是介绍Android Kotlin版封装EventBus,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- Android Kotlin版封装EventBus
- 代码封装
- 添加依赖库
- 定义消息类
- 定义常量值
- 定义注解
- 定义工具类
- 使用
- 在Activity中
- 在Fragment中
- 发送事件
- 源码下载
Android Kotlin版封装EventBus
代码封装
添加依赖库
implementation("org.greenrobot:eventbus:3.3.1")
定义消息类
class MessageEvent<T> {var code: Intvar data: T? = nullconstructor(code: Int) {this.code = code}constructor(code: Int, data: T) {this.code = codethis.data = data}
}
定义常量值
object MessageEventCode {const val REFRESH = 0xB001 //刷新const val DELETE = 0xB002 //删除const val ADD = 0xB003 //增加
}
定义注解
@Target(AnnotationTarget.TYPE, AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class BindEventBus
定义工具类
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中
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中
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)}
}
发送事件
EventBusUtils.post(MessageEvent<Nothing>(MessageEventCode.REFRESH))
EventBusUtils.post(MessageEvent<Nothing>(MessageEventCode.DELETE))
EventBusUtils.post(MessageEvent(MessageEventCode.ADD, "hello world"))
源码下载
这篇关于Android Kotlin版封装EventBus的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!