EventBus3
来源:互联网 发布:剑三帅气成女捏脸数据 编辑:程序博客网 时间:2024/05/22 17:41
1.1 导入组件:
打开App的build.gradle,在dependencies中添加最新的EventBus依赖:
compile 'org.greenrobot:eventbus:3.0.0'
1.2 初始化EventBus
EventBus默认有一个单例,可以通过getDefault()获取,也可以通过EventBus.builder()构造自定义的EventBus,
比如要应用我们生成好的索引时:
EventBus mEventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();
1.3 定义事件:
所有能被实例化为Object的实例都可以作为事件:
public class DriverEvent { public String info; }
1.4 监听事件:
首先把作为订阅事件的模块通过EventBus注册监听:
mEventBus.register(this);
在3.0之前,注册监听需要区分是否监听黏性(sticky)事件,监听EventBus事件的模块需要实现以onEvent开头的方法。如今改为在方法上添加注解的形式:
@Subscribe(threadMode = ThreadMode.POSTING, priority = 0, sticky = true)public void handleEvent(DriverEvent event) { Log.d(TAG, event.info);}
注解有三个参数,threadMode为回调所在的线程,priority为优先级,sticky为是否接收黏性事件。
1.5 发送事件:
调用post或者postSticky即可:
mEventBus.post(new DriverEvent("magnet:?xt=urn:btih……"));
0 0
- EventBus3
- Eventbus3.0
- EventBus3 使用方法
- 初探EventBus3
- EventBus3.0
- EventBus3.0
- EventBus3.0
- EventBus3.0使用总结
- EventBus3.0 总结
- EventBus3.0使用总结
- EventBus3.0使用详解
- EventBus3.0使用
- EventBus3.0的使用
- EventBus3.0学习小结
- EventBus3.0 study
- EventBus3.0源码分析
- EventBus3.0源码分析
- EventBus3.0源码解读
- linux系统函数的饥渴感
- 数组的拷贝
- [CSU 1803 2016]数学
- 参加ThoughtWorks实训——学习经验技巧的小分享(二)
- 配置java环境
- EventBus3
- [LeetCode] 128. Longest Consecutive Sequence
- Java中方法参数的传递
- 初窥 CATransform3D 文档解读
- 射线Physics.Raycast
- mac下安装webpack
- 初窥 CGAffineTransform 文档解读
- jquery.js文件报错missing semicolon
- iOS下的 Fixed + Input 调用键盘的时候fixed无效问题解决方案