美文网首页
Android中使用Kotlin搭建MVP

Android中使用Kotlin搭建MVP

作者: Lkit | 来源:发表于2018-05-01 21:28 被阅读40次

    前言

    从来不写博客的我,突然也想记录一下生活(zhuang bi),于是有了这篇开篇博客😝。如果觉得对你有帮助,请多多支持。虽然kotlin出来有一段时间了,我也是刚正式将kotlin作为Android开发第一语言使用,所以有什么不规范的地方希望各位大佬们能指出。本篇教程适用于有点基础的小白。

    kotlin

    什么是kotlin?kotlin简而言之就是一门基于jvm的编程语言和java兼容,但是比java更强大,而且大大减少你的代码,提高开发效率。

    Mvp

    mvp开发模式已经不是什么新鲜的技术,或者说它也不能说是一门技术。我的理解它就是让代码更加规范的一种编程风格。便于管理和开发,最主要的作用还是用于解耦。由于本篇是教大家如何用Kotlin快速搭建一个Android项目,所以对于一些mvp的概念和kotlin的使用不会做详细的介绍。

    目录结构

    目录结构.png

    以上是我的分包,这是一个Demo,实际项目还是得按具体情况具体分析。

    Base基类

    BaseModle

    interface BaseModle {
        //TODO:modle里相关操作
    }
    

    BaseView

    BaseView
    interface BaseView{
        //TODO:view里相关操作
    }
    

    BasePresenter

    abstract class BasePresenter<V : BaseView> {
        var view: WeakReference<V>? = null
    
        fun bindView(view: V) {
            this.view = WeakReference(view)
        }
    
        fun isBind(): Boolean {
            return view != null && view!!.get() != null
        }
    
        fun unBindView() {
            if (isBind()) {
                view!!.clear()
                view = null
            }
        }
    
        fun obtainView(): V? {
            return if (isBind()) view!!.get() else null
        }
    }
    

    其实和Java抽取mvp基类大同小异,具体问题具体分析,在接口中根据实际情况定义方法。

    上面实现了modle,view,presenter的基类,我们在使用时只需继承即可。

    Example:

    MainContract

    interface MainContract {
        interface IView : BaseView {
            //TODO:...
        }
    
        interface IModle : BaseModle {
            //TODO:...
        }
    
        abstract class IPresenter : BasePresenter<IView>() {
            //TODO:...
        }
    }
    

    MainModle

    class MainModle : MainContract.IModle {
         //TODO:...
    }
    

    MainPresenter

    class MainPresenter : MainContract.IPresenter() {
        init {
            var modle: MainContract.IModle = MainModle()
            var view: MainContract.IView = obtainView()!!
        }
    }
    

    接下来就是Activity的抽取,这个只是一个示例,具体情况还需具体分析
    BaseActivity

    abstract class BaseActivity<V : BaseView, P : BasePresenter<V>> : AppCompatActivity() {
        var presenter: P? = null
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(getResLayoutId())
            presenter = initPresenter()
            presenter!!.bindView(this as V)
            initView()
            initAction()
            doMoreThings()
        }
    
        /**其他操作**/
        abstract fun doMoreThings()
    
        /**获取布局资源**/
        abstract fun getResLayoutId(): Int
    
        /**获取控件**/
        abstract fun initView()
    
        /**设置监听**/
        abstract fun initAction()
    
        /**初始化presenter**/
        abstract fun initPresenter(): P
    
        /**可触摸时执行的操作**/
        abstract fun doOnResume()
    
        override fun onResume() {
            super.onResume()
            doOnResume()
        }
    
        override fun onDestroy() {
            super.onDestroy()
            presenter!!.unBindView()
        }
    }
    

    使用时只需继承即可

    class MainActivity : BaseActivity<MainContract.IView, MainPresenter>(), MainContract.IView {
        override fun doMoreThings() {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }
    
        override fun getResLayoutId(): Int {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }
    
        override fun initView() {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }
    
        override fun initAction() {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }
    
        override fun initPresenter(): MainPresenter {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }
    
        override fun doOnResume() {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }
    
    }
    

    上面给出了Activity的一种抽取方式,Fragment大同小异,就不给出了。

    结语

    至此使用Kotlin搭建一个简单的mvp就已经完成了,其他业务逻辑根据实际情况向接口中添加。本人也刚加入kotlin开发,至此还有很多细节没有注意,欢迎大家吐槽🤣🤣🤣

    相关文章

      网友评论

          本文标题:Android中使用Kotlin搭建MVP

          本文链接:https://www.haomeiwen.com/subject/feufrftx.html