首先搭建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就搭建完成了