原来的库做了升级 需要了解的可以参考新的介绍文章
https://blog.csdn.net/Codyer/article/details/107283919
源码地址 github:https://github.com/codyer/LiveEventBus
基于LiveData,实现eventBus,事件统一管理,动态APT生成,生命周期管理
master
核心代码实现 + demo
demo
通过依赖方式引用master生成的库
combine
通过本地方式依赖master代码
implementation 'com.github.codyer.LiveEventBus:core:1.0.0'
annotationProcessor 'com.github.codyer.LiveEventBus:compiler:1.0.0'
事件定义
@EventScope(name = "demo",active = true)
public enum AppDemo {
@Event(description = "定义一个测试事件",data = String.class)testString,
@Event(description = "定义一个测试事件测试对象",data = TestBean.class)testBean,
}
订阅消息
LiveEventBus.begin()
.inScope(Scope$demo.class)// Scope$***为自动生成的事件接口
.withEvent$testBean()
.observe(this, new ObserverWrapper() {
@Override
public void onChanged(TestBean testBean) {
}
});
LiveEventBus.begin()
.inScope(Scope$demo.class)
.withEvent$testBean()
.observeAny(this, new ObserverWrapper() {
@Override
public void onChanged(@Nullable TestBean testBean) {
}
});
LiveEventBus.begin()
.inScope(Scope$demo.class)
.withEvent$testBean()
.observeForever(new ObserverWrapper() {
@Override
public void onChanged(@Nullable TestBean testBean) {
}
});
取消订阅
LiveEventBus.begin()
.inScope(Scope$demo.class)
.withEvent$testBean().
.removeObserver(observer);
LiveEventBus.begin()
.inScope(Scope$demo.class)
.withEvent$testBean()
.removeObservers(this);
发送消息
LiveEventBus.begin()
.inScope(Scope$demo.class)
.withEvent$testBean()
.setValue(value);
LiveEventBus.begin()
.inScope(Scope$demo.class)
.withEvent$testBean()
.postValue(value);
暂无
compileSdkVersion 28
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName 1.0.0
}
版本 | 功能 |
---|---|
1.0.0 | 初版,支持基本功能 |
源码地址 github:https://github.com/codyer/LiveEventBus
实现原理