(一).前言:
前面我们已经对于AndroidAnnotations框架集成RoboGuice做了讲解,今天我们开始具体学习一下第三方框架集成Otto事件总线。Otto事件总线和我们经常使用Eventbus差不多。Otto 官网: http://square.github.io/otto/,Otto框架的主要功能是帮助我们来降低多个类之间的耦合度的(解耦)。
FastDev4Android框架项目地址:https://github.com/jiangqqlmj/FastDev4Android
(二).集成Otto和AndroidAnnotations
dependencies {
compile 'com.squareup:otto:1.3.8'
}
下面的实例表示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集成已经全部讲解完成了。
同时FastDev4Android项目已经添加配置了AndroidAnnotations框架,后期的框架项目中也会主要使用这个DI框架,.欢迎大家去Github站点进行clone或者下载浏览:https://github.com/jiangqqlmj/FastDev4Android 同时欢迎大家star和fork整个开源快速开发框架项目~