AndroidAnnnotations注入架構使用之第三方框架組成Otto事件匯流排(十四)

來源:互聯網
上載者:User

AndroidAnnnotations注入架構使用之第三方框架組成Otto事件匯流排(十四)

 

 

本文出自:【江清清的部落格】

(一).前言:

前面我們已經對於AndroidAnnotations框架組成RoboGuice做了講解,今天我們開始具體學習一下第三方框架組成Otto事件匯流排。Otto事件匯流排和我們經常使用Eventbus差不多。Otto 官網: http://square.github.io/otto/,Otto架構的主要功能是協助我們來降低多個類之間的耦合度的(解耦)。

 

(二).整合Otto和AndroidAnnotations

  • 整合AndroidAnnotations架構到項目中(具體方法見:【FastDev4Android架構開發】AndroidAnnnotations注入架構介紹和Android Studios基本配置(七)
  • 整合otto架構到項目中(具體請查看http://square.github.io/otto/)這邊講一下AndroidStudio配置吧:

    dependencies {

    compile 'com.squareup:otto:1.3.8'

    }

    • 使用@EBean來為事件匯流排建立一個單例類
    • 建立通過事件匯流排傳輸分發的事件類別
    • 使用bus.post()來進行發送事件到事件匯流排中
    • 使用@Subscribe進行註解來擷取發布的事件

      下面的執行個體表示Fragment進行通知Activity標題發生更新:

       

      // Declare the busas an enhanced bean@EBean(scope =Scope.Singleton)public class OttoBusextends BasicBus { }public classUpdateTitleEvent {         public final String title;         public UpdateTitleEvent(String title) {                this.title = title;        } }

      @EActivity(R.layout.hello_activity)public classHelloAndroidActivity extends FragmentActivity {         @Bean        OttoBus bus;         @Override        protected void onCreate(BundlesavedInstanceState) {               super.onCreate(savedInstanceState);                bus.register(this);        }         @Override        protected void onDestroy() {                super.onDestroy();                bus.unregister(this);        }     @Subscribe    public void onUpdateTitle(UpdateTitleEventevent) {        setTitle(event.title);    } }

      @EFragment(R.layout.hello_fragment)public classHelloFragment extends Fragment {     int counter = 1;     @Bean    OttoBus bus;     @Click    void fragmentButtonClicked() {        bus.post(newUpdateTitleEvent(Clicks:  + counter++));    }}
      到此位置關於AndroidAnnotations第三方框架組成之Otto整合已經全部講解完成了。

       

       

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.