原料:
dagger2Version = "2.5" rxjava2Version = "2.0.1" okhttp3Version = "3.6.0" gsonVersion = "2.8.0"
配置:
//
compile("com.squareup.retrofit2:retrofit:${rootProject.ext.retrofit2Version}") { exclude group: "com.squareup.okhttp3" } compile("com.squareup.retrofit2:converter-gson:${rootProject.ext.retrofit2Version}") { exclude module: "retrofit" }
compile "com.squareup.okhttp3:okhttp:${rootProject.ext.okhttp3Version}" compile "com.squareup.okhttp3:logging-interceptor:${rootProject.ext.okhttp3Version}"
compile "io.reactivex.rxjava2:rxjava:${rootProject.ext.rxjava2Version}" compile("io.reactivex.rxjava2:rxandroid:${rootProject.ext.rxjava2Version}") { exclude module: "android.support" }
compile "com.google.dagger:dagger:${rootProject.ext.dagger2Version}" annotationProcessor "com.google.dagger:dagger-compiler:${rootProject.ext.dagger2Version}"
基本就這些
模塊接口,以登錄模塊舉例
首先:定義接口
public interface MineContract { //ui更新 interface View{ void showList(List<Map<String, Object>> list); void showError(String message); } //module刷新 interface presenter{ void getData(); } }
這兩個可以分開定義,不必要放在一起。這兩個接口是要幹嘛呢?
think in java中有一句話描述接口的:接口規定了可對一個特定的對象發出哪些請求。
interface View是需要activity/fragment來實現的
interface presenter是需要prenester來實現的
2.核心presenter(簡單舉例)
public class MyFriendPresenter implements MineContract.presenter{ private final MineContract.View view; private final SharedPreferences sharedPreferences; private final UserService userService; private String[] friends = {"至尊寶", "牛魔王", "紫霞仙子"}; private Integer[] img = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}; List<Map<String, Object>> list = new ArrayList<>(); //用來提供Presenter的實例化對象 @Inject public MyFriendPresenter(MineContract.View view, @Named("default") SharedPreferences sharedPreferences, UserService userService) { //這個其實就是activity this.view = view; this.sharedPreferences = sharedPreferences; this.userService = userService; } /** * 業務邏輯 */ public void main(){ userService.getUser().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<Response<List<String>>>() { @Override public void accept(Response<List<String>> listResponse) throws Exception { for (int i = 0; i < friends.length; i++) { Map<String, Object> map = new HashMap<>(); map.put("name", friends[i]); map.put("img", img[i]); list.add(map); } //我這裏爲了方便使用的臨時數據, //在presenter裏獲取數據,數據源可以是本地數據庫,遠程數據庫,等 //這個view就是activity引用,請求成功後。執行activity裏面的showList()方法進行頁面刷新 view.showList(list); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { view.showError(throwable.getMessage()); } }); } @Override public void getData() { } }
Component:這個不說了,就是一個紐帶,聯繫依賴和被依賴的關係
核心思想:其實dagger不用也行,但是不使用的話,在activity和presenter之間就是強耦合了。
mvp主要的任務其實就是分解activity的任務,把業務邏輯分離到presenter。然後presenter持有activity的引用。
activity的功能就是ui的刷新。本質上還是接口回調。
接口回調的介紹:
先定義一個接口:
接口a{
打太極()
}
//但是不知道該怎麼打,需要請教類c
類b 實現 接口a{
類c c=new 類c(b)
打太極(){
c.教打太極()
恭喜你,學會了白鶴亮翅,下一次教你猴子偷桃。
}
}
類c{
類c構造器(類b b){
this.類b=b;
}
public void 教太極(){
b.打太極(第一招(白鶴亮翅) )
}
}
簡單的一個僞代碼:
繼續總結:
presenter就是類c專門教武功,處理業務邏輯。(比較叼,但是不想動,持有類b的引用,讓類b去踢館(即更新頁面))
activity就是類b,專門出去踢館,表示老子又剁掉。(但是武功不行,所以需要c來教)
就這樣吧。。需要demo的話,我再放