【56】kotlin搭建mvp。

丶灬走出姿态 提交于 2020-07-25 18:17:22

首先搭建Base.

BaseView

package com.anguo.baselibary.presenter.view

interface BaseView {
    fun showLoading()
    fun hideLoading()
    fun onError()
}

BasePresenter

package com.anguo.baselibary.presenter

import com.anguo.baselibary.presenter.view.BaseView

open class BasePresenter<T:BaseView> {
    lateinit var mView:T
}

实际运行模块 为用户中心  

RegisterView

package com.anguo.user.present.view

import com.anguo.baselibary.presenter.view.BaseView

 interface RegisterView:BaseView {
     fun onRegisterResult(result:Boolean){
     }

}

RegisterPersenter

package com.anguo.user.present

import com.anguo.baselibary.presenter.BasePresenter
import com.anguo.user.present.view.RegisterView

class RegisterPersenter : BasePresenter<RegisterView>() {
    fun register(mobile: String, verifyCode: String) {
        /*业务逻辑*/
        mView.onRegisterResult(true)
    }
}

activity。xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.activity.RegisterActivity">

    <Button
        android:id="@+id/mRegiterBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/register" />
</LinearLayout>

RegisterActivity

package com.anguo.user.ui.activity

import android.os.Bundle
import com.anguo.baselibary.ui.activity.BaseMvpActivity
import com.anguo.user.R
import com.anguo.user.present.RegisterPersenter
import com.anguo.user.present.view.RegisterView
import kotlinx.android.synthetic.main.activity_register.*
import org.jetbrains.anko.toast


class RegisterActivity : BaseMvpActivity<RegisterPersenter>(), RegisterView {

    override fun onRegisterResult(result: Boolean) {
        toast("注册成功")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        mPersenter = RegisterPersenter()
        mPersenter.mView = this;
        mRegiterBtn.setOnClickListener {

            mPersenter.register("", "");
        }
    }
}

这样 基本的一个mvp就搭建完成了

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!