浅谈Android开发模式之RxJava+Retrofit+MVP(kotlin)

此文分两部分讲,一部分是RxJava+Retrofit网络请求的使用,一部分是MVP模式结合网络请求开发

一,RxJava+Retrofit的使用

①module引入

里面已经封装了OkHttp和Gson,不需要另外引入

    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    implementation 'io.reactivex:rxandroid:1.2.1'

②网络层的封装

模块区分类

import com.wocus.wine.entity.MiddlemanBean
import io.reactivex.Observable
import retrofit2.http.POST


interface MainApi {
    @POST("接口地址")
    fun getMiddlemanData():Observable
}

网络请求总入口

package com.wocus.wine.network

import com.wocus.wine.network.api.FileApi
import com.wocus.wine.network.api.MainApi
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory


class Network {
    public val BASEURL="http://域名/"

    private fun getRetrofit():Retrofit = Retrofit.Builder()
            .client(OkHttpClient())
            .baseUrl(BASEURL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()

    public fun manApi():MainApi=getRetrofit().create(MainApi::class.java)

    companion object {
        var netWork: Network? = null

        fun getInstance():Network{
            if (netWork == null) {
                synchronized(Network::class) {
                    if (netWork == null) {
                        netWork = Network()
                    }
                }
            }
            return netWork!!
        }
    }

}

简单请求使用

Network.getInstance().manApi()
                .getMiddlemanData().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    body->
                    ToastUtil.show(baseContext,body.string());
                }

二,RxJava+Retrofit结合MVP的使用

MVP开发模式即
M层负责数据与逻辑的提供者
V层负责界面的更新,如加载框的弹出隐藏,数据显示,动画等等
P层负责结合M层获取数据去调用V层刷新数据

我写了一个简单的例子
View层接口的定义

package com.wocus.wine.view

import com.wocus.wine.entity.*

interface MainView{

    /**
     * 显示轮播图
     */
    fun showImageBanner(listData:MutableList)

    /**
     * 显示文字轮播图
     */
    fun showTextBanner(listData:MutableList)

    /**
     * 显示经纪人RCY
     */
    fun showMiddlemanData(listData:MutableList)

    /**
     * 显示业主房源RCY
     */
    fun showOwnerHouseData(listData:MutableList)

    /**
     * 显示公寓RCY
     */
    fun showApartmentData(listData:MutableList)

    /**
     * 显示公寓房源RCY
     */
    fun showApartmentHouseData(listData:MutableList)

    /**
     * 显示无网络弹框
     */
    fun showNoNetDialog()

    /**
     * 显示服务器异常弹框
     */
    fun showErrorDialog(msg:String)

    /**
     * 显示加载中弹框
     */
    fun showDialog()

    /**
     * 关闭加载中弹框
     */
    fun dismissDialog(num:Int)

    /**
     * 显示添加弹框
     */
    fun showAddDialog()

    /**
     * 旋转
     */
    fun rotateAnim(type:Int)

    /**
     * 消息打印
     */
    fun toast(msg:String)

    /**
     * 关闭刷新
     */
    fun dismissRefresh()

}

Model数据的获取(此处给的是模拟数据的回调)

package com.wocus.wine.model

import com.google.gson.Gson
import com.wocus.wine.callback.HouseCallback
import com.wocus.wine.entity.*
import java.util.*

class MainModel{


    /**
     * 请求后台图片轮播图数据
     */
    fun getImageBannerData(callback:HouseCallback){
        val list = ArrayList()
        list.add("https://bpic.588ku.com/element_banner/20/18/06/56e59bd04e1f1f35afb8a9f26fe469c2.jpg!/unsharp/true/compress/true")
        list.add("https://bpic.588ku.com/element_banner/20/18/06/80c88dd07b1ba5a490471cc828216a7f.jpg!/unsharp/true/compress/true")
        list.add("https://bpic.588ku.com/element_banner/20/18/06/fc0d878aa7fef013a80a6385ab771440.jpg!/unsharp/true/compress/true")
        list.add("https://bpic.588ku.com/element_banner/20/18/06/1a8011cadf7e936d6acceae8f14b7d10.jpg!/unsharp/true/compress/true")
        list.add("https://bpic.588ku.com/element_banner/20/18/06/5669c4f4f49355ed8ddfcebc223dd182.jpg!/unsharp/true/compress/true")
        list.add("https://bpic.588ku.com/element_banner/20/18/06/0317717f3a27c5d5717a42a2390bac2b.jpg!/unsharp/true/compress/true")
        list.add("https://bpic.588ku.com/element_banner/20/18/06/e3743fc3744fc30db419c0ca97bc9f16.jpg!/unsharp/true/compress/true")
        list.add("https://bpic.588ku.com/element_banner/20/18/06/c9a22ed27ff46baf9366e83af9b1bf4f.jpg!/unsharp/true/compress/true")
        callback.onSuccess(Gson().toJson(list))
    }

    /**
     * 请求后台文字轮播图数据
     */
    fun getTextBannerData(callback: HouseCallback){
        val items = ArrayList()
        val item = ItemBean()
        item.title = "最新"
        item.text = "逃离北上广之后,我们可以住哪儿?"+Date().time
        items.add(item)

        val item1 = ItemBean()
        item1.title = "HOT"
        item1.text = "大城市,这里没有生活,但是有钱!"
        items.add(item1)

        val item2 = ItemBean()
        item2.title = "HOT"
        item2.text = "和异性合租是种什么样的体验?"
        items.add(item2)

        val item3 = ItemBean()
        item3.title = "推荐"
        item3.text = "单身小伙把出租屋爆改成五星级公寓!"
        items.add(item3)
        callback.onSuccess(Gson().toJson(items))
    }

    /**
     * 请求经纪人数据
     */
    fun getMiddlemanData(callback: HouseCallback){

       /* Network.getInstance().manApi()
                .getMiddlemanData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(consumer)*/



        val listData=ArrayList()
        listData.add(MiddlemanBean("https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=df67173bdab44aed4d43b6b6d275ec64/2fdda3cc7cd98d1058961840213fb80e7bec9004.jpg","张三","北京,上海,广州,深圳,梧州"))
        listData.add(MiddlemanBean("https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=df67173bdab44aed4d43b6b6d275ec64/2fdda3cc7cd98d1058961840213fb80e7bec9004.jpg","张三","北京,上海,广州,深圳,梧州"))
        listData.add(MiddlemanBean("https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=df67173bdab44aed4d43b6b6d275ec64/2fdda3cc7cd98d1058961840213fb80e7bec9004.jpg","张三","北京,上海,广州,深圳,梧州"))
        listData.add(MiddlemanBean("https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=df67173bdab44aed4d43b6b6d275ec64/2fdda3cc7cd98d1058961840213fb80e7bec9004.jpg","张三","北京,上海,广州,深圳,梧州"))
        listData.add(MiddlemanBean("https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=df67173bdab44aed4d43b6b6d275ec64/2fdda3cc7cd98d1058961840213fb80e7bec9004.jpg","张三","北京,上海,广州,深圳,梧州"))
        callback.onSuccess(Gson().toJson(listData))
    }

    /**
     * 请求业主房源数据
     */
    fun getOwnerHouseData(callback: HouseCallback){
        val listData=ArrayList()
        listData.add(OwnerHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","新安街道 锦华大厦设备全齐","3室2厅2卫-99㎡","宝安区-新安街道","9800元/月"))
        listData.add(OwnerHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","新安街道 锦华大厦设备全齐","3室2厅2卫-99㎡","宝安区-新安街道","9800元/月"))
        listData.add(OwnerHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","新安街道 锦华大厦设备全齐","3室2厅2卫-99㎡","宝安区-新安街道","9800元/月"))
        listData.add(OwnerHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","新安街道 锦华大厦设备全齐","3室2厅2卫-99㎡","宝安区-新安街道","9800元/月"))
        listData.add(OwnerHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","新安街道 锦华大厦设备全齐","3室2厅2卫-99㎡","宝安区-新安街道","9800元/月"))
        listData.add(OwnerHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","新安街道 锦华大厦设备全齐","3室2厅2卫-99㎡","宝安区-新安街道","9800元/月"))
        callback.onSuccess(Gson().toJson(listData))
    }

    /**
     * 请求品牌公寓数据
     */
    fun getApartmentData(callback: HouseCallback){
        val listData=ArrayList()
        listData.add(ApartmentBean("http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","梧桐公寓"))
        listData.add(ApartmentBean("http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","梧桐公寓"))
        listData.add(ApartmentBean("http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","梧桐公寓"))
        listData.add(ApartmentBean("http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","梧桐公寓"))
        listData.add(ApartmentBean("http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","梧桐公寓"))
        listData.add(ApartmentBean("http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","http://image.mgzf.com/mogoroom/2018-02/mogoBrand/9/0/2003559/2003559_1517817412022.jpg","梧桐公寓"))
        callback.onSuccess(Gson().toJson(listData))
    }

    /**
     * 请求公寓房源数据
     */
    fun getApartmentHouseData(callback: HouseCallback){
        val listData=ArrayList()
        listData.add(ApartmentHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","福田区-锦州大厦","2室2厅1卫-70㎡","6200元/月"))
        listData.add(ApartmentHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","福田区-锦州大厦","2室2厅1卫-70㎡","6200元/月"))
        listData.add(ApartmentHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","福田区-锦州大厦","2室2厅1卫-70㎡","6200元/月"))
        listData.add(ApartmentHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","福田区-锦州大厦","2室2厅1卫-70㎡","6200元/月"))
        listData.add(ApartmentHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","福田区-锦州大厦","2室2厅1卫-70㎡","6200元/月"))
        listData.add(ApartmentHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","福田区-锦州大厦","2室2厅1卫-70㎡","6200元/月"))
        listData.add(ApartmentHouseBean("http://image.mgzf.com/mogoroom/2018-03/mogoApp/7/1/3100687/3100687_1520229043848.jpg!mobile","福田区-锦州大厦","2室2厅1卫-70㎡","6200元/月"))
        callback.onSuccess(Gson().toJson(listData))
    }
}

Presenter层的实现

package com.wocus.wine.presenter

import com.wocus.wine.callback.HouseCallback
import com.wocus.wine.entity.*
import com.wocus.wine.model.MainModel
import com.wocus.wine.util.GsonUtil
import com.wocus.wine.view.MainView

class MainPresenter() {
    private lateinit var mainView:MainView

    constructor(mainView: MainView) : this() {
        this.mainView=mainView
        mainView.showDialog()
        getImageBanner()
        getTextBanner()
        getMiddleman()
        getOwnerHouse()
        getApartment()
        getApartmentHouse()
    }

    private fun getImageBanner() = MainModel().getImageBannerData(object:HouseCallback{
        override fun onSuccess(data: String) {
                mainView.showImageBanner(GsonUtil.GsonToList(data,String::class.java))
                mainView.dismissDialog(1)
        }

        override fun onFailure(msg: String) =mainView.showErrorDialog(msg)

        override fun onError() =mainView.showNoNetDialog()
    })

    private fun getTextBanner()=MainModel().getTextBannerData(object :HouseCallback{

        override fun onSuccess(data: String){
            mainView.showTextBanner(GsonUtil.GsonToList(data,ItemBean::class.java))
            mainView.dismissDialog(1)
        }

        override fun onFailure(msg: String) = mainView.showErrorDialog(msg)

        override fun onError() = mainView.showNoNetDialog()
    })

    private fun getMiddleman()=MainModel().getMiddlemanData(object :HouseCallback{
        override fun onSuccess(data: String) {
            mainView.showMiddlemanData(GsonUtil.GsonToList(data,MiddlemanBean::class.java))
            mainView.dismissDialog(1)

        }

        override fun onFailure(msg: String) =mainView.showErrorDialog(msg)

        override fun onError() =mainView.showNoNetDialog()
    })

    private fun getOwnerHouse()=MainModel().getOwnerHouseData(object :HouseCallback{
        override fun onSuccess(data: String) {
            mainView.showOwnerHouseData(GsonUtil.GsonToList(data,OwnerHouseBean::class.java))
            mainView.dismissDialog(1)
        }

        override fun onFailure(msg: String) = mainView.showErrorDialog(msg)

        override fun onError() = mainView.showNoNetDialog()
    })

    private fun getApartment()=MainModel().getApartmentData(object :HouseCallback{
        override fun onSuccess(data: String) {
            mainView.showApartmentData(GsonUtil.GsonToList(data, ApartmentBean::class.java))
            mainView.dismissDialog(1)
        }

        override fun onFailure(msg: String) = mainView.showErrorDialog(msg)

        override fun onError() =mainView.showNoNetDialog()
    })

    private fun getApartmentHouse()=MainModel().getApartmentHouseData(object:HouseCallback{
        override fun onSuccess(data: String) {
            mainView.showApartmentHouseData(GsonUtil.GsonToList(data, ApartmentHouseBean::class.java))
            mainView.dismissDialog(1)
        }

        override fun onFailure(msg: String) =mainView.showErrorDialog(msg)

        override fun onError() =mainView.showNoNetDialog()
    })

    public fun showAddDialog()=mainView.showAddDialog()

    public fun rotateAnim(type:Int)=mainView.rotateAnim(type)

    public fun refresh(){
        MainPresenter(mainView)
        mainView.dismissRefresh()
    }
}

MVP需要的已经搭建完毕,接下来就用在Activity或者Fragment上就OK了
贴出Fragment的代码

package com.wocus.wine.fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wocus.wine.R;
import com.wocus.wine.activity.HouseDetailsActivity;
import com.wocus.wine.activity.PayActivity;
import com.wocus.wine.adapter.ApartmentAdapter;
import com.wocus.wine.adapter.ApartmentHouseAdapter;
import com.wocus.wine.adapter.MiddlemanAdapter;
import com.wocus.wine.adapter.OwnerHouseAdapter;
import com.wocus.wine.adapter.TextVerticalAdapter;
import com.wocus.wine.baseview.VerticalScrollLayout;
import com.wocus.wine.entity.ApartmentBean;
import com.wocus.wine.entity.ApartmentHouseBean;
import com.wocus.wine.entity.ItemBean;
import com.wocus.wine.entity.MiddlemanBean;
import com.wocus.wine.entity.OwnerHouseBean;
import com.wocus.wine.presenter.MainPresenter;
import com.wocus.wine.util.GlideImageLoader;
import com.wocus.wine.util.ToastUtil;
import com.wocus.wine.view.MainView;
import com.youth.banner.Banner;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Objects;

public class MainFragment extends Fragment implements MainView,BaseQuickAdapter.OnItemClickListener,View.OnClickListener,SwipeRefreshLayout.OnRefreshListener {

    private Banner banner;
    private VerticalScrollLayout vScrollLayout;
    private RecyclerView rcyMiddleman;
    private RecyclerView rcyOwnerHouse;
    private RecyclerView rcyApartment;
    private RecyclerView rcyApartmentHouse;
    private MiddlemanAdapter middlemanAdapter;
    private OwnerHouseAdapter ownerHouseAdapter;
    private ApartmentAdapter apartmentAdapter;
    private ApartmentHouseAdapter apartmentHouseAdapter;
    private LinearLayoutManager linearLayoutManager;
    private ProgressDialog progressDialog;
    private MainPresenter presenter;
    private SwipeRefreshLayout srl;
    private TextView txtArea;
    private TextView txtSearch;
    private ImageView imgAdd;
    private int num=0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view=inflater.inflate(R.layout.fragment_main,null);
        banner=view.findViewById(R.id.banner);
        vScrollLayout = view.findViewById(R.id.scroll_layout);
        rcyMiddleman=view.findViewById(R.id.main_rcy_middleman);
        rcyOwnerHouse=view.findViewById(R.id.main_rcy_owner_house);
        rcyApartment=view.findViewById(R.id.main_rcy_apartment);
        rcyApartmentHouse=view.findViewById(R.id.main_rcy_apartment_house);
        txtArea=view.findViewById(R.id.main_txt_area);
        txtSearch=view.findViewById(R.id.main_txt_search);
        imgAdd=view.findViewById(R.id.main_img_add);
        srl=view.findViewById(R.id.mian_srl);
        view.findViewById(R.id.main_layout_1).setOnClickListener(this);
        view.findViewById(R.id.main_layout_2).setOnClickListener(this);
        view.findViewById(R.id.main_layout_3).setOnClickListener(this);
        view.findViewById(R.id.main_layout_4).setOnClickListener(this);
        view.findViewById(R.id.main_layout_5).setOnClickListener(this);
        view.findViewById(R.id.main_layout_6).setOnClickListener(this);
        view.findViewById(R.id.main_layout_7).setOnClickListener(this);
        view.findViewById(R.id.main_layout_8).setOnClickListener(this);
        view.findViewById(R.id.main_layout_middleman_add).setOnClickListener(this);
        view.findViewById(R.id.main_layout_owner_house).setOnClickListener(this);
        view.findViewById(R.id.main_layout_apartment).setOnClickListener(this);
        view.findViewById(R.id.main_layout_apartment_add).setOnClickListener(this);
        view.findViewById(R.id.main_layout_apartment_house).setOnClickListener(this);
        imgAdd.setOnClickListener(this);
        txtArea.setOnClickListener(this);
        txtSearch.setOnClickListener(this);
        vScrollLayout.setOnClickListener(this);
        progressDialog=new ProgressDialog(this.getContext());
        progressDialog.setMessage("加载中...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        presenter=new MainPresenter(this);
        srl.setOnRefreshListener(this);
        return view;
    }

    @Override
    public void showImageBanner(List listData) {
        banner.setImages(listData).setImageLoader(new GlideImageLoader()).start();
    }

    @Override
    public void showTextBanner(List listData) {

        TextVerticalAdapter adapter=new TextVerticalAdapter(getContext(),listData);
        vScrollLayout.setAdapter(adapter);
        adapter.setData(listData);
    }

    @Override
    public void showMiddlemanData(List listData) {
        linearLayoutManager=new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rcyMiddleman.setLayoutManager(linearLayoutManager);
        middlemanAdapter=new MiddlemanAdapter(R.layout.item_middleman,listData);
        rcyMiddleman.setAdapter(middlemanAdapter);
        middlemanAdapter.setOnItemClickListener(this);
    }

    @Override
    public void showOwnerHouseData(List listData) {
        linearLayoutManager=new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rcyOwnerHouse.setLayoutManager(linearLayoutManager);
        ownerHouseAdapter=new OwnerHouseAdapter(R.layout.item_owner_house,listData);
        rcyOwnerHouse.setAdapter(ownerHouseAdapter);
        ownerHouseAdapter.setOnItemClickListener(this);
    }

    @Override
    public void showApartmentData(List listData) {
        linearLayoutManager=new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rcyApartment.setLayoutManager(linearLayoutManager);
        apartmentAdapter=new ApartmentAdapter(R.layout.item_apartment,listData);
        rcyApartment.setAdapter(apartmentAdapter);
        apartmentAdapter.setOnItemClickListener(this);
    }

    @Override
    public void showApartmentHouseData(List listData) {
        linearLayoutManager=new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rcyApartmentHouse.setLayoutManager(linearLayoutManager);
        apartmentHouseAdapter=new ApartmentHouseAdapter(R.layout.item_apartment_house,listData);
        rcyApartmentHouse.setAdapter(apartmentHouseAdapter);
        apartmentHouseAdapter.setOnItemClickListener(this);
    }

    @Override
    public void showNoNetDialog() {
        ToastUtil.show(getContext(),"请检查你的网络");
    }

    @Override
    public void showErrorDialog(String msg) {
        ToastUtil.show(getContext(),msg);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (Objects.equals(adapter.getClass().getName(), MiddlemanAdapter.class.getName())){
            ToastUtil.show(getContext(),"经纪人");
        }else if(Objects.equals(adapter.getClass().getName(), OwnerHouseAdapter.class.getName())){
            ToastUtil.show(getContext(),"房主房源");
            startActivity(new Intent(getContext(),HouseDetailsActivity.class));
        }else if(Objects.equals(adapter.getClass().getName(), ApartmentAdapter.class.getName())){
            ToastUtil.show(getContext(),"公寓");
        }else if(Objects.equals(adapter.getClass().getName(), ApartmentHouseAdapter.class.getName())){
            ToastUtil.show(getContext(),"公寓房源");
        }
    }

    @Override
    public void showDialog() {
        progressDialog.show();
    }

    @Override
    public void dismissDialog(int num) {
        this.num+=num;
        if (this.num>=6){
            progressDialog.dismiss();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.main_layout_1:{
                ToastUtil.show(getContext(),"整租");
                startActivity(new Intent(getContext(),PayActivity.class));
                break;
            }
            case R.id.main_layout_2:{
                ToastUtil.show(getContext(),"合租");
                break;
            }
            case R.id.main_layout_3:{
                ToastUtil.show(getContext(),"独栋公寓");
                break;
            }
            case R.id.main_layout_4:{
                ToastUtil.show(getContext(),"特惠专区");
                break;
            }
            case R.id.main_layout_5:{
                ToastUtil.show(getContext(),"业主房源");
                break;
            }
            case R.id.main_layout_6:{
                ToastUtil.show(getContext(),"品牌公寓");
                break;
            }
            case R.id.main_layout_7:{
                ToastUtil.show(getContext(),"交通");
                break;
            }
            case R.id.main_layout_8:{
                ToastUtil.show(getContext(),"地图");
                break;
            }
            case R.id.scroll_layout:{
                ToastUtil.show(getContext(),"新闻");
                break;
            }
            case R.id.main_layout_middleman_add: {
                ToastUtil.show(getContext(), "入住经纪人");
                break;
            }
            case R.id.main_layout_owner_house: {
                ToastUtil.show(getContext(), "业主房源");
                break;
            }
            case R.id.main_layout_apartment:{
                ToastUtil.show(getContext(),"品牌公寓");
                break;
            }
            case R.id.main_layout_apartment_add:{
                ToastUtil.show(getContext(),"品牌公寓入住");
                break;
            }
            case R.id.main_layout_apartment_house:{
                ToastUtil.show(getContext(),"品牌公寓房源");
                break;
            }
            case R.id.main_txt_area:{
                ToastUtil.show(getContext(),"切换地区");
                break;
            }
            case R.id.main_txt_search:{
                ToastUtil.show(getContext(),"搜索房源");
                break;
            }
            case R.id.main_img_add:{
                presenter.showAddDialog();
                break;
            }

        }
    }

    @Override
    public void rotateAnim(int type) {
        Animation anim =new RotateAnimation(type==1?0f:45f,type==1?45f:0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setFillAfter(true); // 设置保持动画最后的状态
        anim.setDuration(200); // 设置动画时间
        anim.setInterpolator(new AccelerateInterpolator()); // 设置插入器
        imgAdd.startAnimation(anim);
    }

    @Override
    public void showAddDialog() {
        presenter.rotateAnim(1);
        View view=getLayoutInflater().inflate(R.layout.layout_main_add,null);
        PopupWindow popupment=new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
        popupment.setBackgroundDrawable(new ColorDrawable(Color.argb(0,0,0,0)));
        popupment.setOutsideTouchable(true);
        popupment.setTouchable(true);
        popupment.setAnimationStyle(R.style.animTranslate);
        popupment.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                presenter.rotateAnim(0);
            }
        });
        popupment.showAsDropDown(imgAdd,Gravity.RIGHT,0,0 );
    }

    @Override
    public void onRefresh() {
        presenter.refresh();
    }

    @Override
    public void toast(@NotNull String msg) {
        ToastUtil.show(getContext(),msg);
    }

    @Override
    public void dismissRefresh() {
        srl.setRefreshing(false);
    }
}

做起来就是这样,Fragment实现View层接口,Fragment事件订阅Presenter层,Presenter层调用Model层获取数据,再调用View层更新数据,是不是很简单呢?

你可能感兴趣的:(浅谈Android开发模式之RxJava+Retrofit+MVP(kotlin))