EventBus-訂閱總線

介紹EventBus

EventBus是一種用於Android的事件發佈-訂閱總線,它簡化了應用程序內各個組件之間進行通信的複雜度,尤其是碎片之間進行通信的問題,可以避免由於使用廣播通信而帶來的諸多不便。

在EventBus中有三個重點
1、發送者
用來在任意線程中,發送數據,參數是Objcet類型.
2、接收者
用來接收數據.但是接收之前需要在當前的頁面註冊和註銷,

@Override
public void onStart() {
    super.onStart();
    //訂閱事件
    EventBus.getDefault().register(this);
}

@verride
public void onStop() {
    super.onStop();
    //退訂事件
    EventBus.getDefault().unregister(this);
}

3、事件

需要發送的內容,這裏也可以封裝成一個對象

@Subscribe(threadMode = ThreadMode.MAIN)
public void getMsg(EvenBus e){
    tvShow.setText(e.getText());
}

這裏被@Subscribe註解的方法可以理解爲一個容器,裏面存放的就是我們期望發送的數據或對象內容
關於後面的括號裏的內容是一個設置線程的方法模型

POSTING: 默認,表示事件處理函數的線程跟發佈事件的線程在同一個線程。
MAIN: 表示事件處理函數的線程在主線程(UI)線程,因此在這裏不能進行耗時操作。
BACKGROUND: 表示事件處理函數的線程在後臺線程,因此不能進行UI操作。如果發佈事件的線程是主線程(UI線程),那麼事件處理函數將會開啓一個後臺線程,如果果發佈事件的線程是在後臺線程,那麼事件處理函數就使用該線程。
ASYNC: 表示無論事件發佈的線程是哪一個,事件處理函數始終會新建一個子線程運行,同樣不能進行UI操作

案例:通過EventBus給Fragment設置EditText傳來的值

Activity佈局文件:

<?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=".MainActivity">

    <EditText
        android:id="@+id/et_text"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <Button
        android:id="@+id/btn_send"
        android:text="send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <fragment
        android:id="@+id/fg"
        android:name="com.example.eventbus.BlankFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Activity代碼:

public class MainActivity extends AppCompatActivity {
    private EditText et_text;
    private Button btnSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_text = (EditText) findViewById(R.id.et_text);
        btnSend = (Button) findViewById(R.id.btn_send);


        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Editable text = et_text.getText();
                EvenBus evenBus = new EvenBus();
                evenBus.setText(text.toString());
                EventBus.getDefault().post(evenBus);
            }
        });
    }
}

Fragment佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".BlankFragment">

    <TextView
        android:id="@+id/tv_show"
        android:text="這是fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</FrameLayout>

Fragment代碼:

/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment extends Fragment {
    private TextView tvShow;

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
        tvShow = (TextView) inflate.findViewById(R.id.tv_show);

        return inflate;
    }

    @Override
    public void onStart() {
        super.onStart();
        //訂閱事件
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMsg(EvenBus e){
        tvShow.setText(e.getText());
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}

官方網址

https://github.com/greenrobot/EventBus
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章