android MVP模式使用

model 负责数据获取

class ListModel {
    fun getListData(page:Int,listener:ListLoadingListener){
        Handler(Looper.getMainLooper()).postDelayed({
            var arrayList = ArrayList<ListBean>()
            for (item in 1..10){
                arrayList.add(ListBean("pageList=>${item}", "page*item",page.toString()))
            }
            listener.loadSuccess(arrayList)
        },1000)
    }
}

interface ListLoadingListener{
    fun loadSuccess(listData:ArrayList<ListBean>)
    fun loadError()
}

viewI 界面的动作接口

interface ListViewI {
    fun getCurrPage():Int
    fun setCurrPageData(listData:ArrayList<ListBean>)
    fun toRefreshAt()
    fun toUpLoadAt()
}

presenter 处理view跟model的逻辑

class ListPresenter {

    var iView:ListViewI
    var listModel: ListModel
    constructor(iView:ListViewI){
        this.iView = iView
        listModel = ListModel()
    }

    fun getListData(){
    	//model获取数据;而请求条件、处理结果是通过iView在activity的实现去处理
        listModel.getListData(iView.getCurrPage(),object :ListLoadingListener{
            override fun loadSuccess(listData: ArrayList<ListBean>) {
                iView.setCurrPageData(listData)
            }

            override fun loadError() {
                //TODO error handler
            }
        })
    }
}

activity 实现动作接口

class ListActivity : BaseActivity(), ListViewI {
    private var currNum: Int = 1;
    var pageList = ArrayList<ListBean>();
    private lateinit var listAdapter:ListAdapter
    private lateinit var mListPresenter:ListPresenter
    private lateinit var moreModule: BaseLoadMoreModule
    val mThis by lazy { this }
    override fun setLayout(): Int {
        return R.layout.activity_list
    }

    override fun initView() {

        listAdapter = ListAdapter(R.layout.item_list,pageList)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = listAdapter

        refreshLayout.setOnRefreshListener {
            toRefreshAt()
            refreshLayout.isRefreshing = false
        }
        moreModule = listAdapter.loadMoreModule
        moreModule.setOnLoadMoreListener {
            toUpLoadAt()
        }
        listAdapter.setOnItemClickListener { adapter, view, position ->
            Toast.makeText(mThis, "go go go-> $position", Toast.LENGTH_SHORT).show()
        }
        mListPresenter = ListPresenter(this)
        mListPresenter.getListData()
    }

    override fun getCurrPage(): Int {
        return currNum
    }

    override fun setCurrPageData(listData:ArrayList<ListBean>)  {
        //刷新界面
        if (currNum==1){
            pageList.clear()
            pageList.addAll(listData)
            listAdapter.notifyDataSetChanged()
        }else{
            listAdapter.addData(listData)
        }
        moreModule.loadMoreComplete()
    }

    override fun toRefreshAt() {
        currNum = 1
        mListPresenter.getListData()
    }

    override fun toUpLoadAt() {
        currNum++
        mListPresenter.getListData()
    }
}

你可能感兴趣的:(android,java,kotlin)