您的位置:首页 > 编程语言 > Java开发

手把手带你走进MVP +Dagger2 + DataBinding+ Rxjava+Retrofit 的世界

2017-12-01 11:52 585 查看
0.0 Android开发现在的变化用一个词来形容就是 :翻天覆地 



越来越多的项目使用了MVP + Rxjava+Retrofit +Dagger2 + DataBinding等等东西。。 但是这些东西对于木有用过的同学们开起来还是比较头疼的。 

转载请标明出处:http://blog.csdn.net/wingichoy/article/details/51981756 

网上有很多介绍他们的教程,但是都比较详细(我听到有童鞋问:详细还不好?? 其实他们最好的学习方式还是边敲边踩坑边学,一下介绍的太详细,对于初学者只能有 懵逼的效果),所以准备写一篇比较简单的博客,来介绍这些东西的简单用法。大家跟着敲,即使是初学者应该也不会很懵逼。

开始之前先说说这些东西的作用

1.MVP 大家都知道 P的作用是让MV间接拥有肮脏的PY交易,而不是直接让他们进行交易。 

2.Rxjava 响应式编程 0.0 一个特别屌的地方就是你可以随便切换线程 

3.Retrofit 代替Volley的东东 

4.Dagger2 Android 的IOC框架,即控制反转,也叫依赖注入 

4.DataBinding MVVM的东东,用起来比较方便,可以让bean与View绑定,抛弃setText()!

东西比较多,但是

憋怕! 我们个一个来了解他们最简单的用法。

告诉你们个秘密,其实



MVP

MVP 在http://blog.csdn.net/wingichoy/article/details/50893367有简单的介绍,新司机们可以先去看看。 

以往的MVP 都要定义3个接口 分别是 IModel IView IPresenter。 写多了你就会发现,太特么占地方了。。。 这里介绍一种解决办法,就是引用一种契约类。说白了就是三个接口放一起。
public interface MainContract {
//View 的接口
interface MainView{
void showData(ApiBean.RetDataBean retDataBean);
void showProgressBar();
}
//presenter的接口
interface MainPresenter{
void getMainData();
}
//Model的接口
interface MainModel{
Observable loadMainData();
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

在MVP中 M负责业务逻辑(进行io操作) V就是Activity,负责与用户交互,P就是他俩搞基的桥梁。好比牛郎(M)织女(V),牛郎要和织女见面,必须有桥(P)啊。


Dagger2

Dagger就是依赖注入,解耦用的。常见的使用地方就是注入Presenter到Activity中。

例子如下
public class MainActivity extends AppCompatActivity implements MainContract.MainView {
MainContract.MainPresenter mPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//mPresenter 不再以new的形式创建
mPresenter = DaggerMainComponent.builder().mainModule(new MainModule(this)).build().getPresenter();
1
2
3
4
5
6
7
8

这样做有什么好处呢,比如现在的presenter需要一个参数来构造,到后面需求改了,需要两个了,如果使用new。 只要修改Presenter 又得修改 Activity,0.0 耦合度太!高!了! 如果采用这种方式,就可以愉快的修改Presenter,不管Presenter怎么修改,Activity的代码都不需要改变了。


使用Dagger2的方法


1.配置

在app的gradle里加入:
compile "com.google.dagger:dagger:2.4"
apt "com.google.dagger:dagger-compiler:2.4"
provided 'org.glassfish:javax.annotation:10.0-b28'
1
2
3

最顶部加入
apply plugin: 'com.neenbedankt.android-apt'
1

在project的gradle加入
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
1


2.使用

还是以刚才的文件为例子,我们想要注入presenter,那么久在presenter的构造器添加注解@Inject
public class MainPresenterImpl implements MainContract.MainPresenter {
private MainContract.MainView mMainView;
private MainContract.MainModel mMainModel;

@Inject
public MainPresenterImpl(MainContract.MainView mainView) {
mMainView = mainView;
mMainModel = new MainModelImpl();
}
1
2
3
4
5
6
7
8
9
10

之后构建一个Module,用来提供依赖(presenter构造器需要的参数)
@Module
public class MainModule {
private MainContract.MainView mMainView;
//Module的构造器,传入一个MainView, 提供给Component
public MainModule(MainContract.MainView mMainView) {
this.mMainView = mMainView;
}
//Provides注解代表提供的参数,为构造器传进来的
@Provides
public MainContract.MainView inject(){
return mMainView;
};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

之后构建一个Component,注意上面的注解,代表使用MainModule
@Component(modules = MainModule.class)
public interface MainComponent {
MainPresenterImpl getPresenter();
}
1
2
3
4
5

最后在Activity里进行注入操作
mPresenter = DaggerMainComponent.builder().mainModule(new MainModule(this)).build().getPresenter();
1

直的注意的是 这个Dagger开头的Component是在Build的过程中创建的,如果第一次书写 ,请先Rebuild。 这样就完成了presenter的注入。看到这里你可能已经懵逼了,没关系,照着多敲几遍,俗话说, 代码百遍,其义自现。 其实就是使用Component创建一个Presenter,而Presenter所需的参数都是由Moudule提供的。


DataBinding


1.配置

在app的gradle里加入
compile "com.jakewharton.rxbinding:rxbinding:0.4.0"
compile "com.jakewharton.rxbinding:rxbinding-design:0.4.0"
1
2

在andrid{}中加入
compileSdkVersion 24
buildToolsVersion "24.0.0"
dataBinding {
enabled = true
}
}
1
2
3
4
5
6


2.使用方法

首先要改变的是布局文件,布局以layout为跟布局
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

tools:context=".view.activity.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{phone.phone}" />
</RelativeLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

然后在代码里 取代系统本身的setContentView(); 之后就可以对布局对象进行操作了。
public class MainActivity extends AppCompatActivity implements MainContract.MainView {
private ActivityMainBinding mBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
mBinding.tvTest.setText("hello");
}
1
2
3
4
5
6
7
8
9


3.将数据绑定

在layout标签下 多了个data可以定义引入类。比如有一个实体RetDataBean,里面存放了 phone 等信息,就可以直接在View上显示实体的信息。
<data>

<variable
name="phone"
type="com.wingsofts.rxretrofitmvpdagger2databinding.bean.ApiBean.RetDataBean" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{phone.phone}" />
</RelativeLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

上面代表定义一个RetDataBean类变量,名称为phone,在android:text里面去引用他。 

并且在Activity中去把数据给加进去
@Override
public void showData(ApiBean.RetDataBean retDataBean) {
mBinding.setPhone(retDataBean);
}
1
2
3
4

这就是DataBinding的简单使用


Rxjava


1.配置

在app的gradle里加入如下代码
compile "io.reactivex:rxandroid:1.2.0"
compile "io.reactivex:rxjava:1.1.7"
1
2


2.使用

简单的说一下基本语法。 其实Rx就是Observable和Subscriber之间的肮脏PY交易。 不认识这俩单词的童鞋可以先敲个100遍混脸熟。不要怕,就是刚。Observable是事件源,Subscriber是订阅者,当事件源发生时间的时候,subscriber做出相应的反应,可以拿去和OnClickListener作对比。 看一个简单的栗子:
sampleobservalbe.subscribe(new Subscriber<ApiBean>() {
@Override
public void onCompleted() {
//当所有onNext()执行完毕后处罚
}

@Override
public void onError(Throwable e) {
//错误的时候触发
}

@Override
public void onNext(ApiBean apiBean) {
//当sampleObservable发生事件的时候触发
Log.e("wing","onNext");
}

});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

以上代码就是 一个简单的Rxjava使用语法。 当sampleObservable事件发生的时候,调用new Subscriber的onNext方法。 此时你可能有疑惑,这个sampleObservable哪里来的,当然是键盘敲出来的了! 在和Retrofit搭配使用的时候,Retrofit的API会返回一个Observable给你,当然网络的访问不能再MainThread上,别问我为啥。然后Rxjava的强大之处就体现出来了,可以随意切换线程! 比如我们拿到Retrofit返回的observable,想让他在工作线程访问网络,直接调用subscribeOn(Schedulers.io()),在拿到数据回到Subscriber的时候,只需要调用 

observeOn(AndroidSchedulers.mainThread())即可,完全抛弃了什么Handler有木有!!!屌不屌!! 所以以上叙述的代码如下:
mMainModel.loadMainData()
.observeOn(AndroidSchedulers.mainThread())//拿到数据的时候在主线程
.subscribeOn(Schedulers.io())//访问数据在工作线程
.subscribe(new Subscriber<ApiBean>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(ApiBean apiBean) {
mMainView.showData(apiBean.getRetData());
Log.e("wing","onNext");
}

});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Rxjava的简单用法就介绍到这里,因为本文的主旨是让读者亲自轻松敲起来,在敲代码的过程中踩坑,在踩坑的过程中成长。如果需要了解更多,推荐:扔物线大神的这篇文章


Retrofit


1.配置

在app的gradle加入如下代码
compile "com.squareup.retrofit2:retrofit:2.1.0"
compile "com.squareup.retrofit2:converter-gson:2.1.0"
compile "com.squareup.retrofit2:adapter-rxjava:2.1.0"
compile "com.squareup.okhttp3:3.4.1"
compile "com.squareup.okhttp3:logging-interceptor:3.4.1"
compile 'com.google.code.gson:gson:2.4'
1
2
3
4
5
6
7


2.使用

前面说了,Retrofit可以根据接口返回一个Observable对象,下面看看如何使用。 

比如我要访问的接口为:http://wifikey.org/api/api.php 

首先,用GsonFormat生成一个Bean, 

之后定义如下接口
public interface DataApi {
@GET("api.php")Observable<ApiBean> getData();
}
1
2
3
4

在代码里使用Retrofit生成retrofit对象
//Constant.HOST =http://wifikey.org/api/
mApi = new Retrofit.Builder().baseUrl(Constant.HOST).addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build().create(DataApi.class);
1
2
3
4

此时DataApi就创建成功了,调用DataApi的getData()方法,会返回一个Observable对象,接下来怎么做,你知道了吧。


到此,MVP +Dagger2 + DataBinding+ Rxjava+Retrofit 的简单介绍就完成了。


希望大家多多动手敲,敲完之后,网上有很多优秀的单个介绍的文章,努力去踩坑,才能成长。


如果你喜欢我的博客,可以关注我,评论我,不嫌多,蟹蟹!

点击下载项目
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: