EventBus
来源:互联网 发布:云计算saas实验 编辑:程序博客网 时间:2024/05/29 19:24
概述
EventBus是事件订阅、发布的事件总线。github:https://github.com/greenrobot/EventBus。分为订阅者、发布者、事件、总线 四部分。
publisher:发布者subscriber:订阅者
见图:
步骤:
- 注册:订阅者订阅事件(就是可以接收到事件总线发出的事件)
- 发布:发布者发布事件(发布者发布事件到事件总线)
- 取消注册:订阅者取消订阅事件(不再接收事件总线发出的事件)
相关的方法:
1 EventBus.getDefault().register(this);//订阅者订阅事件
2 发布事件
MyEvent event = new MyEvent();event.setType("0");event.setMessage("0内容");EventBus.getDefault().post(event);//发布事件
3 EventBus.getDefault().unregister(this);//订阅不再消订阅事件
下面通过一个demo来演示:页面有1个Button和1个TextView通过点击Button改变textview的值。
代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.cqc.eventbus2.MainActivity"> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="50dp" android:text="btn"/> <TextView android:id="@+id/tv" android:layout_below="@id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!"/> </RelativeLayout>
MainActivity.java
package com.cqc.eventbus2; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; public class MainActivity extends Activity { private TextView tv; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this);//注册事件到订阅者 btn = (Button) findViewById(R.id.btn); tv = (TextView) findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyEvent event = new MyEvent(); event.setType("0"); event.setMessage("0内容"); EventBus.getDefault().post(event);//发布事件 } }); } //订阅事件处理的4种方式 // @Subscribe // public void onEvent(MyEvent event){ // if (event.getType().equals("0")) { // tv.setText(event.getMessage()); // } // } // @Subscribe // public void onEventMainThread(MyEvent event) { // if (event.getType().equals("0")) { // tv.setText(event.getMessage()); // } // } // // @Subscribe // public void onEventPostThread(MyEvent event) { // if (event.getType().equals("0")) { // tv.setText(event.getMessage()); // } // } // @Subscribe // public void onEventBackgroundThread(MyEvent event){ // if (event.getType().equals("0")) { // tv.setText(event.getMessage()); // } // } // @Subscribe // public void onEventAsync(MyEvent event){ // if (event.getType().equals("0")) { // tv.setText(event.getMessage()); // } // } //3.0之后,不受方法名影响,但是需要用public + @Subscribe修饰 @Subscribe public void abc(MyEvent event) { if (event.getType().equals("0")) { tv.setText(event.getMessage()); } } @Override protected void onDestroy() { super.onDestroy(); //取消注册 EventBus.getDefault().unregister(this); } }
MyEvent.java
package com.cqc.eventbus2;public class MyEvent { private String type; private String message; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; }}
点击下载源码:https://github.com/s1168805219/EventBusDemo
0 0
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- Eventbus
- EventBus
- EventBus
- EventBus
- EventBus
- Timer和TimerTask
- logback系列之八:自定义Pattern模板
- HTTP状态码大全
- Eclipse在debug模式下不显示变量值的解决办法
- Mac adb command not found
- EventBus
- 深入理解spring
- android 使用Glide实现listview图片缓存
- 图片问题
- android.graphics.Paint类详解
- 安卓ListView根据Adapter动态设置高度
- Objective-C延迟执行方法总结
- I00036 盈数(Abundant number)
- ionic开发——点击发送短信功能