教你从0到1搭建一个完整的电商app---(MVP封装)

上一篇我们已经创建好了基本的项目,一个App主工程,BaseLibrary和Provider两个基本Module,这三个部分我们以后可以在所有的项目中直接使用,这也是模块化管理的好处。 这篇博客主要介绍如何封装一个最基本的MVP框架,我们都知道MVP模式在Android开发中被大量使用,这样能够将业务逻辑和视图显示区分开,降低耦合性,提高代码可读性。接下来就让我们看具体的代码吧 。

interface BaseView {
    fun showLoading()
    fun hideLoading()
    fun onError(text: String)
}
open class BasePresenter<T: BaseView> {

    lateinit var mView: T
 
}
open class BaseActivity:AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

}
open class BaseMvpActivity<T : BasePresenter<*>> : BaseActivity(), BaseView {

    lateinit var mPresenter: T

    override fun hideLoading() {
    }

    override fun onError(text: String) {
    }

    override fun showLoading() {
    }

}

我们使用泛型封装了BaseActivity和BasePresenter,接下来我们创建一个UserCenter个人中心登陆界面来使用一下我们的代码


interface LoginView : BaseView {
    fun loginResult(result: Boolean)
}
class LoginPresenter : BasePresenter<LoginView>() {

    fun login(account: String,password:String){
        if (account == "admin" && password == "123456"){
            mView.loginResult(true)
        }else{
            mView.loginResult(false)
        }
    }
}
class LoginActivity : BaseMvpActivity<LoginPresenter>(), LoginView, View.OnClickListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        mPresenter = LoginPresenter()
        mPresenter.mView = this
        mLoginBtn.setOnClickListener(this)

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.mLoginBtn -> {
                mPresenter.login(mAccountEt.text.toString(), mPassWordEt.text.toString())
            }
        }
    }

    override fun loginResult(result: Boolean) {
        if (result) {
            toast("登陆成功")
        } else {
            toast("登陆失败")
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.kotlin.usercenter.ui.activity.LoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号" />

        <EditText
            android:id="@+id/mAccountEt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码" />

        <EditText
            android:id="@+id/mPassWordEt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <Button
        android:id="@+id/mLoginBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="登陆" />
</LinearLayout>

到此我们MVP的简单封装就完成了,接下来我们将引入Dagger2,使用注入的方式来优化代码。
附上项目github地址

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值