簡單的Mvp設計

任務:從網絡上獲取數據,而後顯示在MainActivity的ListView上html

1、載入須要用的框架

一、Mvp框架java

compile 'com.hannesdorfmann.mosby:mvp:2.0.1'react

compile 'com.hannesdorfmann.mosby:viewstate:2.0.1'android

二、Retrofit和RxJava框架json

compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.5'
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta1'設計模式

2、設計草圖api

根據Mvp設計模式:網絡

將提供數據的網絡請求,放入model包中。框架

將顯示屏幕數據的類,放入View包中ide

將處理數據和轉換的邏輯,放入Presents包中。

咱們這裏:只須要實現當獲取到數據時候,ListView的顯示狀況就能夠了。

3、開工

一、建立Retrofit的網絡請求   Retrofit的使用  放到Model這個包下面

public class ArticleEntity{
   //將json數據轉換爲實體類
    private String title;
    private String detail;

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
ArticleEntity
public interface DataApi {
//發送數據請求
    @GET("refreshlayout/api/newdata{pageNumber}.json")
    Observable<List<ArticleEntity>> getArticles(@Path("pageNumber") int pageNumber);
}
DataApi

最後:建立

public class HttpConnection {
    //地址:從網上找的
    private static final String BASE_URL = "http://7xk9dj.com1.z0.glb.clouddn.com/";
    private static HttpConnection mHttConnection;
    private Retrofit mRetrofit;
    //建立Retrofit
    private HttpConnection(){
        mRetrofit = new Retrofit.Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build();
    }
    //使用單例模式,加上雙層鎖
    public static synchronized HttpConnection newInstace(){
        if (mHttConnection == null){
            synchronized (HttpConnection.class){
                mHttConnection = new HttpConnection();
            }
        }
        return mHttConnection;
    }
    //獲取數據的方法,將subscriber做爲回調接口
    public void getData(int pageNum,Subscriber subscriber){
        DataApi api = mRetrofit.create(DataApi.class);
        Observable observable = api.getArticles(pageNum);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
}
httpConnection

二、建立View

分析:根據咱們的需求,咱們只須要屏幕顯示數據這一個動做就能夠了,因此咱們設計的View是這樣的

//實現:建立接口並繼承接口MvpView
public interface IArticleView extends MvpView{
    //獲取到數據,顯示
    void show(ArrayList<ArticleEntity> articleItem);
}
IArticle

三、設計Present類

一樣,咱們也只須要顯示方法就能夠了

public class ArticlePresent extends MvpBasePresenter<IArticleView> {
    private int mViewPage = 0;
    private HttpConnection mConnection;
    @Override
    public void attachView(IArticleView view) {
        super.attachView(view);
        mConnection = HttpConnection.newInstace();
    }
    //獲取數據,並處理,而後傳遞給View的中轉站
    public void show(){
        //獲取上步驟的View
        final IArticleView view = getView();
        if (view != null){
            Subscriber subscriber = new Subscriber() {
                @Override
                public void onCompleted() {
                    Log.d("MainActivity","完成了");
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(Object o) {
                    //當獲取到了數據,就調用View的方法
                    view.show((ArrayList<ArticleEntity>)o);
                }
            };
            //獲取相關數據
            mConnection.getData(mViewPage,subscriber);
        }
    }

}
ArticlePresnt

四、MainActivity顯示數據

BaseMvpActivity  繼承MvpBaseActivity 設計了一個基礎封裝方法,封裝了一些基礎方法,

泛型:是在類名後建立的        泛型是在方法的返回值前建立的

public abstract class BaseMvpActivity <V extends MvpView,T extends MvpPresenter<V>>extends MvpActivity<V,T> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView(savedInstanceState);
        setListener();
    }

    public abstract void initView(Bundle savedInstanceState);

    public abstract void setListener();

    public <VT extends View> VT getViewById(int id){
        return (VT) findViewById(id);
    }
}
BaseMvpActivity
public class MainActivity extends BaseMvpActivity<IArticleView,ArticlePresent> implements IArticleView{
    private ListView mListView;
    private ArticleAdapter mArticleAdapter;
    @NonNull
    @Override
    public ArticlePresent createPresenter() {
        return new ArticlePresent();
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mListView = getViewById(R.id.main_listView);
        mArticleAdapter = new ArticleAdapter(this,new ArrayList<ArticleEntity>());
        mListView.setAdapter(mArticleAdapter);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void show(ArrayList<ArticleEntity> articleItem) {
        mArticleAdapter.addArticles(articleItem);
    }
}
MainActivity

步驟:繼承BaseMvpActivity,繼承剛纔的IAcrticle接口

 

設計方式:將獲取數據的類裝入model中,而後指定View顯示的接口方法(好比說:沒數據時候顯示什麼界面,有數據的時候顯示什麼界面,裏面的數據是否是不同了)

在Present完成處理數據和根據不一樣數據調用View方法的邏輯。

相關文章
相關標籤/搜索