EventBus传值及生成索引

来源:互联网 发布:淘宝商品详情页面html 编辑:程序博客网 时间:2024/06/03 15:26

在project的build.gradle下配置:

 

 dependencies {        classpath 'com.android.tools.build:gradle:3.0.1'        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'   //EventBus配置        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }

在App的build.gradle下配置:

 

android {    defaultConfig {        javaCompileOptions {            annotationProcessorOptions {                arguments = [ eventBusIndex : 'com.example.myapp.MyEventBusIndex' ]            }        }    }}dependencies {    compile 'org.greenrobot:eventbus:3.0.0'    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.0.1'}

在MyApp extends  Application中注册配置:

EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();
MyEventBusIndex的文件必须在注册、订阅和发送完成的时候才会生成

 

(1)自定义一个类,可以是空类,比如:

[java] view plain copy
  1. public class AnyEventType {  
  2.      public AnyEventType(){}  
  3.  }  

(2)在要接收消息的页面注册:

[java] view plain copy
  1. eventBus.register(this);  

(3)发送消息

[java] view plain copy
  1. eventBus.post(new AnyEventType event);  

(4)接受消息的页面实现(共有四个函数,各功能不同,这是其中之一,可以选择性的实现,这里先实现一个):

[java] view plain copy
  1. public void onEvent(AnyEventType event) {}