Android中使用開源架構EventBus3.0實現Fragment之間的通訊互動,fragment開源架構

來源:互聯網
上載者:User

Android中使用開源架構EventBus3.0實現Fragment之間的通訊互動,fragment開源架構
1.概述

在之前的博文中簡單介紹過如何?fragment之間的資訊互動:《Android中Fragment與Activity之間的互動(兩種實現方式)》,今天繼續給大家介紹一種可以實現此效果的另外一種方式EventBus。(相比於handler,介面回調,bundle傳參,這個簡單好用到哭)

EventBus是Android下高效的發布/訂閱事件的訊息匯流排。作用是可以代替傳統的Intent,Handler,Broadcast或介面函數在Fragment、Activity、Service、線程之間傳遞資料進行通訊,執行方法。做為訊息匯流排,有三個主要元素:

(1)Event:事件

(2)Subscriber:事件訂閱者,接受特定的事件

(3)Publisher:事件發行者,用於通知Subscriber有事件發生

結合EventBus以上的三個元素,我們也可以稱其為一種觀察者設計模式。

EventBus 官網連結http://greenrobot.org/eventbus/

EventBus GitHub連結https://github.com/greenrobot/EventBus

前期相關博文連結:

Android中Fragment與Activity之間的互動(兩種實現方式)

Android中Fragment的兩種建立方式

2.Demo樣本(1)樣本中左側的按鈕,潘侯爺與碧空海觸發的事件為EventBus的普通事件發布(2)左側粘性事件按鈕發布的為粘性事件3.實現步驟

本次Demo架構:

3.1導依賴包

使用AndroidStudio2.2。仍然採用在build.gradle下中dependencies下直接添加如下代碼:

compile 'org.greenrobot:eventbus:3.0.0'

同步後完成依賴添加。

3.2布局檔案

(1)layout中主布局檔案,activity_main.xml檔案

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    tools:context="com.mly.panhouye.eventbustest.MainActivity">    <LinearLayout        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:orientation="vertical"        android:background="#6f6669">        <Button            android:layout_gravity="center_horizontal"            android:id="@+id/panhouye"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="ŋ" />        <Button            android:layout_gravity="center_horizontal"            android:id="@+id/bikonghai"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="̿պ" />        <Button            android:layout_gravity="center_horizontal"            android:id="@+id/postSticky"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="ճДʂ" />    </LinearLayout>    <FrameLayout        android:id="@+id/framelayout"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="2"></FrameLayout></LinearLayout>

(2)layout中右側的fragment布局檔案fragment_msg.xml檔案

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/tv"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="no data"        android:textSize="50sp"        android:gravity="center_horizontal"/></LinearLayout>

(3)layout中粘性事件的示範介面布局activity_main2.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:id="@+id/activity_main2"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.mly.panhouye.eventbustest.Main2Activity">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:textSize="30sp"        android:gravity="center_horizontal"        android:id="@+id/tv"        android:text="no data"/></RelativeLayout>
3.3java實現代碼

(1)自訂事件類別

本次示範最簡單事件的發布,事件僅發布字串資料,MessageEvent.java檔案如下:

package com.mly.panhouye.eventbustest;/** * Created by panchengjia on 2017/2/19 0019. */public class MessageEvent {    String data;    public MessageEvent(String data) {        this.data = data;    }}

(2)MsgFragment.java

右側fragment對應的java類,除了在其中關聯其對應的fragment布局外,還需要添加修改fragment中文本的方法,如下:

package com.mly.panhouye.eventbustest;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;/** * Created by panchengjia on 2017/2/20 0020. */public class MsgFragment extends Fragment {    TextView tv;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_msg,container,false);        tv = (TextView) view.findViewById(R.id.tv);        return view;    }    public void setText(String message){        tv.setText(message);    }}

(3)MainActivity.java

MainActivity.java對應的布局為主布局,右側的fragment附屬於該布局,所以需要在該類中註冊EventBus,將當前的Activity註冊為事件訂閱者,具體代碼如下:

package com.mly.panhouye.eventbustest;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;import org.greenrobot.eventbus.ThreadMode;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    Button panhouye,bikonghai,postSticky;    MsgFragment msgFragment;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        panhouye= (Button) findViewById(R.id.panhouye);        bikonghai= (Button) findViewById(R.id.bikonghai);        postSticky= (Button) findViewById(R.id.postSticky);        panhouye.setOnClickListener(this);        bikonghai.setOnClickListener(this);        postSticky.setOnClickListener(this);        //添加fragment到右側的幀布局中        msgFragment = new MsgFragment();        getSupportFragmentManager().beginTransaction().add(R.id.framelayout,msgFragment).commit();    }    /*個人建議在onResume註冊EventBus     *在可見可互動狀態下註冊,儘可能少的佔用記憶體     */    @Override    protected void onResume() {        super.onResume();        EventBus.getDefault().register(this);    }    /*個人建議在onPause註冊EventBus(將當前Activity註冊為事件訂閱者)     *不影響功能的情況下提早解除註冊,儘可能少的佔用記憶體     */    @Override    protected void onPause() {        super.onPause();        EventBus.getDefault().unregister(this);    }    /**     * 事件發行者(通過按鈕點擊事件進行事件發布)     * @param v     */    @Override    public void onClick(View v) {        switch (v.getId()){            //(1)事件發布中所傳參數可以作為右側fragment文本的修改內容            //(2)事件發布中所傳參數也可以用作事件訂閱者執行方法的區分通知            case R.id.panhouye:                EventBus.getDefault().post(new MessageEvent("潘侯爺"));                break;            case R.id.bikonghai:                EventBus.getDefault().post(new MessageEvent("碧空海"));                break;            case R.id.postSticky:                //粘性事件發布                EventBus.getDefault().postSticky(new MessageEvent("粘性事件"));                startActivity(new Intent(this,Main2Activity.class));                break;        }    }    /**     * 事件訂閱者自訂的接收方法     * @param event     */    @Subscribe(threadMode = ThreadMode.MAIN)    public void onMessageEvent(MessageEvent event) {//        //(1)將事件發行者發布的資料作為文本修改內容//        msgFragment.setText(event.data);        //(2)將事件發行者發布的資料作為方法執行的區分        switch(event.data){            case "潘侯爺":                msgFragment.setText("panhouye");                break;            case "碧空海":                msgFragment.setText("bikonghai");                break;        }    }}

(4)Main2Activity.java

注意:此布局作為粘性事件發布的訂閱者,同樣需要註冊EventBus

package com.mly.panhouye.eventbustest;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;import org.greenrobot.eventbus.ThreadMode;public class Main2Activity extends AppCompatActivity {    TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        tv = (TextView) findViewById(R.id.tv);    }    @Override    protected void onResume() {        super.onResume();        EventBus.getDefault().register(this);    }    @Override    protected void onPause() {        super.onPause();        EventBus.getDefault().unregister(this);    }    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)    public void onMessageEvent(MessageEvent event) {//        //(1)將事件發行者發布的資料作為文本修改內容        tv.setText(event.data);        //(2)將事件發行者發布的資料作為方法執行的區分//        switch(event.data){//            case "粘性事件"://                tv.setText("panhouye");//                break;//        }    }}

發布的粘性事件在其新訂閱者註冊後將會自動傳遞給新訂閱者,有時我們也需要移除粘性事件,以免它在傳遞下去。

MessageEvent stickyEvent = EventBus.getDefault().getStickyEvent(MessageEvent.class);// Better check that an event was actually posted beforeif(stickyEvent != null) {      // "Consume" the sticky event      EventBus.getDefault().removeStickyEvent(stickyEvent);      // Now do something with it}MessageEvent stickyEvent = EventBus.getDefault().removeStickyEvent(MessageEvent.class);// Better check that an event was actually posted beforeif(stickyEvent != null) {      // Now do something with it}
4.線程模式

EventBus提供了四種線程模式:

(1)postThread:使用者將被調用在同一個線程中,這是發布事件(這是預設值)。事件傳遞意昧著最少的開銷,因為它完全避免了線程切換。因此,這是推薦的模式,來處理簡單的任務,如果是已知的完成是一個很短的時間,而不需要主線程。事件處理使用此模式必須迅速返回,以避免阻塞發布線程,這可能是主線程。

(2)MainThread:使用者將被調用在主線程(UI線程)。如果發布線程是主線程,事件處理常式方法將直接調用。使用此模式的事件處理常式必須快速返回,避免阻塞主線程。

(3)BackgrounThread:將在後台線程中調用訂閱者。如果發布線程不是主線程,則事件處理常式方法將被在發布線程中直接調用。如果線程是主線程,eventbus採用單獨的一個後台線程,將按順序調用所有的事件。使用此模式的事件處理常式應嘗試快速返回,以避免阻塞後台線程。

(4)Async:事件處理常式方法在一個單獨的線程中調用。這總是獨立於發布線程和主線程。發布事件從來不會等待使用這種模式的事件處理常式方法。事件處理常式方法使用此模式,如果他們的執行可能需要一段時間,例如用於網路訪問。避免觸發大量在同一時間運行長時間啟動並執行非同步處理常式方法以限制並發線程的數目。eventbus使用一個線程池來有效地重用已完成的非同步事件處理常式通知的線程。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.