文章目录
- Android Kotlin版封装EventBus
-
Android Kotlin版封装EventBus
代码封装
添加依赖库
implementation("org.greenrobot:eventbus:3.3.1")
定义消息类
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
}
}
定义常量值
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)
}
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"))