美文网首页
Kotlin实现MVP+Okhttp3.0+Retorfit2.

Kotlin实现MVP+Okhttp3.0+Retorfit2.

作者: 职度 | 来源:发表于2019-04-16 20:22 被阅读0次
    案例图1 案例图1

    项目数据列表展示

    MVP 架构 分包

    mvp架构图

    Contract 契约接口代码块实现

    
    /**
     * @copyright:  zhidu
     * @author   fanyanlong
     * @email  326445811@qq.com
     * @versions:  V1.0.1
     * @data: 2019/4/16
     * @description:
     *
     */
    interface Contract {
        //Iview
        interface Iview {
            fun GetViewDate(mktbean: KtBean)
        }
    
        // Ipresenter
        interface Ipresenter {
            fun GetPersnetViewDate(page: Int, count: Int)
            fun Attch(miview: Iview)
            fun Decth()
        }
    
        // Imodel
        interface Imodel {
            fun GetModelDate(page: Int, count: Int, model: ModelCallBack)
            interface ModelCallBack {
                fun onSuccess(mttbena: KtBean)
                fun onFails()
            }
        }
    }
    
    
    

    View层代码块实现

    **
     * @copyright:  zhidu
     * @author   fanyanlong
     * @email  326445811@qq.com
     * @versions:  V1.0.1
     * @data: 2019/4/16
     * @description:
     *
     */
    open class KtActivity : AppCompatActivity(), Contract.Iview {
        var ktpresenter: KtPresenter? = null
        var adapter: KtAdapter? = null
        var page: Int = 1
        var count: Int = 3
        var mHandler: Handler? = Handler()
        var array: ArrayList<Any>? = ArrayList()
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_kt)
            tv.text = "职业渡人"
            ktpresenter = KtPresenter(this)
            ktpresenter!!.Attch(this@KtActivity)
            //添加布局管理器
            kt_xrecyclerview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
    
            var block: (KtPresenter?) -> Unit = {
                it!!.Attch(this)
                it.GetPersnetViewDate(page, count)
            }
            ktpresenter.let(block)
    
            kt_xrecyclerview.setLoadingMoreEnabled(true)
            kt_xrecyclerview.setPullRefreshEnabled(true)
            kt_xrecyclerview.setLoadingMoreProgressStyle(ProgressStyle.BallZigZag)
            kt_xrecyclerview.setLoadingListener(object : XRecyclerView.LoadingListener {
                override fun onLoadMore() {
    
                    //kt_xrecyclerview.postDelayed(Runnable { kotlin.run {  } },3000)
                    mHandler!!.postDelayed(object : Runnable {
                        override fun run() {
                            page++
                            Log.i("onLoadMore===", page.toString())
                            // ktpresenter!!.Attch(this@KtActivity)
                            ktpresenter!!.GetPersnetViewDate(page, count)
                            adapter!!.notifyDataSetChanged()
                            kt_xrecyclerview.loadMoreComplete()
    
                        }
                    }, 1000)
    
    
                }
    
                override fun onRefresh() {
                    page = 1
                    // ktpresenter!!.Attch(this@KtActivity)
                    Log.i("onRefresh===", page.toString())
                    ktpresenter!!.GetPersnetViewDate(page, count)
                    adapter!!.notifyDataSetChanged()
                    kt_xrecyclerview.refreshComplete()
    
                }
            })
        }
    
        override fun GetViewDate(mktbean: KtBean) {
            adapter = KtAdapter(this@KtActivity, mktbean)
            kt_xrecyclerview.adapter = adapter
        }
    
        override fun onDestroy() {
            super.onDestroy()
            ktpresenter!!.Decth()
    
        }
    
    }
    

    Presenter 代码块实现

    
    /**
     * @copyright:  zhidu
     * @author   fanyanlong
     * @email  326445811@qq.com
     * @versions:  V1.0.1
     * @data: 2019/4/16
     * @description:
     *
     */
    open class KtPresenter(val mview: KtActivity) : Contract.Ipresenter {
        var mmodel: KtModel? = KtModel()
        //?可为空
        override fun Attch(miview: Contract.Iview) {
            //软引用
           // soft = SoftReference<Contract.Imodel>(mmodel)
        }
    
        override fun Decth() {
            if (mview != null) {
                mview == null
            }
        }
    
        override fun GetPersnetViewDate(page: Int, count: Int) {
            //!!表示对象不为空的情况下去执行
            mmodel!!.GetModelDate(page, count, object : Contract.Imodel.ModelCallBack {
                override fun onSuccess(mttbena: KtBean) {
                    mview.GetViewDate(mttbena)
                }
    
                override fun onFails() {
                }
            })
    
    
        }
    }
    

    Model 代码块实现

    
    /**
     * @copyright:  zhidu
     * @author   fanyanlong
     * @email  326445811@qq.com
     * @versions:  V1.0.1
     * @data: 2019/4/16
     * @description:
     *
     */
    //open可被继承
    class KtModel : Contract.Imodel {
        //override重写  fun方法
        override fun GetModelDate(page: Int, count: Int, model: Contract.Imodel.ModelCallBack) {
            //获取网络
            RetrofitUtils.instance.create.getNetDate(page, count)
                    //切换io流
                    .subscribeOn(Schedulers.io())
                    //切换主线程
                    .observeOn(AndroidSchedulers.mainThread())
                    //订阅 object对象变量
                    .subscribe(object : DisposableObserver<KtBean>() {
                        //完成操作
                        override fun onComplete() {
    
                        }
    
                        //下一步操作
                        override fun onNext(t: KtBean) {
                            model.onSuccess(t)
                        }
    
                        //错误操作
                        override fun onError(e: Throwable) {
                        }
    
                    })
        }
    }
    
    
    

    总结

    上面主要以项目主干模块代码一个整理,如果想实现此效果请关注私信反馈问题,线上解决
    此案例以分享github上面链接如下:

    Kotlin项目实战篇【1】Android Studio 3.0 Kotlin环境开发https://github.com/fanyanlong/KotlinStu

    Kotlin基础篇【1】Android Studio 3.0 Kotlin环境配置
    Kotlin基础篇【2】初识Kotlin的意义
    Kotlin基础篇【3】Kotlin基础语法
    Kotlin基础篇【4】when,if,in,is 关键词

    =========Kotlin项目实战【2】即将更新==========

    ===========约努力的人约幸运==========

    相关文章

      网友评论

          本文标题:Kotlin实现MVP+Okhttp3.0+Retorfit2.

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