來不及解釋了,快上車之EventBus3.0快速上手
快速教你上手EventBus3.0,在EventBus3.0之前用法不同,就不在這里說了。
準備工作,建立EventBus3.0的依賴:
compile 'org.greenrobot:eventbus:3.0.0'
基本使用
1.在需要訂閱的組件內注冊事件:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
message = (TextView) this.findViewById(R.id.message);
EventBus.getDefault().register(this);
}
2.在結束的時候注銷事件:
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
3.定義接受事件的方法,可以看到下面代碼的源注釋,當需要定義接受方法的時候,就需要如此申明
threadMode = ThreadMode.MAIN 表示接受的方法發生的線程,其他線程指定的方式在下一章具體解釋。
注意:參數不支持基本數據類型,想用整型必須使用Integer:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onBus(String msg){
message.setText(msg);
}
4.發送事件參數,發送的參數不支持基本數據類型:
public void oneView(View v){
EventBus.getDefault().post("開車了");
}
以上就是EventBus的基本使用了:下面介紹一下簡單的用法:
用法一,同一組件內發送和接收事件
package com.jelly.eventbus;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class MainActivity extends AppCompatActivity {
private TextView message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
message = (TextView) this.findViewById(R.id.message);
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
/**
* 同一界面內傳遞字符串
* @param msg
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onBus(String msg){
message.setText(msg);
}
public void oneView(View v){
EventBus.getDefault().post("同一界面內點擊");
}
}
在Activity的生命周期onCreate方法中注冊事件,在onDestroy()方法中注銷事件,寫了一個在主線程中執行的接收事件的方法修改TextView的值,點擊按鈕是發送事件。
用法二,事件傳遞自定義的對象
定義一個Bean
public class Message {
public String message;
public Message(String message) {
this.message = message;
}
}
事件接收和發送邏輯,在這里發送的是自定義的對象
package com.jelly.eventbus;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class MainActivity extends AppCompatActivity {
private TextView message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
message = (TextView) this.findViewById(R.id.message);
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
/**
* 傳遞對象
* @param msg
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onBus(Message msg){
message.setText(msg.message);
}
public void postObj(View v){
EventBus.getDefault().post(new Message("傳遞對象"));
}
}
用法三,在不同的組件之間發送和接收事件
接收事件的Activity
package com.jelly.eventbus;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
message = (TextView) this.findViewById(R.id.message);
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
/**
* 不同組件之間傳遞數據
* @param i
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onBus(Integer i){
Log.v("bus",i+"");
}
public void secondView(View v){
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
}
}
發送事件的Activity,剛開始寫的時候在發送事件的Activity里面內也加上的注冊和注銷操作,然后運行失敗,在發送事件的組件內不需要在注冊和注銷EventBus
package com.jelly.eventbus;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import org.greenrobot.eventbus.EventBus;
/**
* Created by Jelly on 2016/9/27.
*/
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_second);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public void twoCon(View view){
Log.v("bus","點擊");
EventBus.getDefault().post(1);
}
}
在接收事件的Activity中啟動發送事件的Activity,點擊其中的按鈕,打印的日志結果如下
來自:http://www.jianshu.com/p/57208945a9ab
本文由用戶 aevd9798 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!