EventBus事件匯流排(個人理解監聽回撥,勿噴)
阿新 • • 發佈:2019-01-28
不詳解,只介紹用法(包括主執行緒呼叫EventBus,子執行緒呼叫EventBus)
步驟:
1、需要接收事件處註冊EventBus,如在Main中註冊
EventBus.getDefault().register(this);
2、新建事件類
3、需要傳送事件傳送傳送事件,如SecondActivity中傳送
EventBus.getDefault().post(new SecondFButtonClickEvent(mEtInput.getText().toString()));
4、需要接收事件處註冊實現事件接收(需要手動打出來,注意@Subscribe註釋)*子執行緒中返回的時間需要在主執行緒中操作時需要宣告位置@Subscribe public void onEvent(SecondFButtonClickEvent event) { // UI updates must run on MainThread mTextView.setText(event.msg); }
@Subscribe(threadMode = ThreadMode.MAIN)
程式碼如下:1、事件接收處
佈局package com.example.leixiansheng.eventbustest; 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.Button; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity { @BindView(R.id.button) Button mButton; @BindView(R.id.textView) TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); EventBus.getDefault().register(this); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SecondActivity.class)); } }); mTextView.setText("等待EventBus回傳"); } @Subscribe public void onEvent(SecondFButtonClickEvent event) { // UI updates must run on MainThread mTextView.setText(event.msg); } @Subscribe public void onEvent(SecondOnCreateEvent event) { Log.i("TAG", event.msg); } @Subscribe public void onEvent(SecondOnDestroyEvent event) { Log.i("TAG", event.msg); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }
2、事件傳送處<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.leixiansheng.eventbustest.MainActivity"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
package com.example.leixiansheng.eventbustest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.Timer;
import java.util.TimerTask;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by Leixiansheng on 2018/4/9.
*/
public class SecondActivity extends AppCompatActivity {
@BindView(R.id.btn_back)
Button mBtnBack;
@BindView(R.id.et_input)
EditText mEtInput;
@BindView(R.id.btn_thread)
Button mBtnThread;
@BindView(R.id.iv_cover)
ImageView mIvCover;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ButterKnife.bind(this);
EventBus.getDefault().register(this);
EventBus.getDefault().post(new SecondOnCreateEvent("second onCreate"));
mBtnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = mEtInput.getText().toString();
boolean isStrEmpty = str.isEmpty() || (str.length() < 0);
if (isStrEmpty) {
Toast.makeText(SecondActivity.this, "請輸入返回內容", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(SecondActivity.this, "已經返回資料到主頁面", Toast.LENGTH_SHORT).show();
//通過EventBus.getDefault().post();向主頁傳輸資料
EventBus.getDefault().post(new SecondFButtonClickEvent(mEtInput.getText().toString()));
mBtnBack.setEnabled(false);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
finish();
}
}, 1000);
}
});
mBtnThread.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
EventBus.getDefault().post(new RefreshCoverEvent(R.mipmap.ic_launcher));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
/**
* 子執行緒資料載入完成後在UI介面重新整理時,需要選擇threadMode
* @param event
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(RefreshCoverEvent event) {
mIvCover.setImageResource(event.ivId);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().post(new SecondOnCreateEvent("second onDestroy"));
EventBus.getDefault().unregister(this);
}
}
佈局<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.leixiansheng.eventbustest.MainActivity">
<Button
android:id="@+id/btn_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="back"/>
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="請輸入回傳內容"
android:gravity="left"
android:textColorHint="#ffffffff"
android:background="#000000"
android:textColor="#ffffffff" />
<Button
android:id="@+id/btn_thread"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create Thread"
android:textAllCaps="false"/>
<ImageView
android:id="@+id/iv_cover"
android:layout_gravity="center"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
3、自定義的事件類package com.example.leixiansheng.eventbustest;
/**
* Created by Leixiansheng on 2018/4/9.
*/
public class SecondFButtonClickEvent {
public String msg;
public SecondFButtonClickEvent(String msg) {
this.msg = msg;
}
}
package com.example.leixiansheng.eventbustest;
/**
* Created by Leixiansheng on 2018/4/9.
*/
public class SecondOnCreateEvent {
public String msg;
public SecondOnCreateEvent(String msg) {
this.msg = msg;
}
}
package com.example.leixiansheng.eventbustest;
/**
* Created by Leixiansheng on 2018/4/9.
*/
public class SecondOnDestroyEvent {
public String msg;
public SecondOnDestroyEvent(String msg) {
this.msg = msg;
}
}
Log列印資訊如下
04-11 16:14:29.411 27433-27433/com.example.leixiansheng.eventbustest I/TAG: second onCreate
04-11 16:15:18.991 27433-27433/com.example.leixiansheng.eventbustest I/TAG: second onDestroy
詳解可參考一下網址