用Kotlin+Retrofit+RxKotlin+MVP撸了一个小项目

4,946 阅读2分钟

作为Android开发者,对于谷歌大大力推的Kotlin肯定是要会用的啦。 最近就使用了豆瓣电影的部分接口撸了一个小Demo来学习。(由于电脑比较渣,开了模拟器录屏好卡。其实在手机上还是很流畅的。哈哈哈)

简介

遵循MD风格,采用MVP架构,使用了Kotlin+Retrofit+RxKotlin开发。现在主要有两个功能:

  • 豆瓣电影正在上映列表与详情(由于API的限制,部分功能未实现)
  • 使用方向传感器与矩阵实现了图片随手机摆动而移动位置(需要真机)

关于第一个功能

Kotlin+Retrofit+RxKotlin 一起使用
  • 在ApiServers中定义请求方法
@GET(BaseURL.HOTSCREEN)
    fun getHotScreenList(@QueryMap par: HashMap<String, String>): Observable<HotScreenResult>
  • 在module层中封装
override fun loadHotScreenData(page: Int, next: (result: HotScreenResult) -> Unit
                                   , error: (th: Throwable) -> Unit) {
        val parm = HashMap<String, String>()
        parm.put("apikey", getApiKey())
        parm.put("city", "深圳")
        parm.put("start", page.toString())
        parm.put("count", "10")
        parm.put("client", "")
        parm.put("udid", getToken())
        BuildApi.buildApiServers()!!
                .getHotScreenList(parm)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeBy(
                        onNext = next,
                        onError = error)
    }

Presenter层调用

 if (view == null || module == null)
            return
    view?.showLoading()
    module?.loadHotScreenData(page, {
        view!!.notifyList(it)
        view!!.dismissLoading()
        }, {
        it.printStackTrace()
        view!!.dismissLoading()
        view!!.showTipMessage(1, "网络异常")})
共享元素转场动画
  • 在要转场的xml文件中分别定义相同的transitionName
<!-- 第一个页面 -->
<ImageView
            android:id="@+id/item_hot_screen_image"
            android:layout_width="@dimen/dimen_100"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:transitionName="@string/t_movie_list_to_detail"/>


<!-- 第二个页面 -->
<ImageView
                android:id="@+id/movie_detail_coll_head_photo"
                android:layout_width="@dimen/dimen_150"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="@dimen/dimen_15"
                android:layout_marginTop="@dimen/dimen_55"
                android:scaleType="centerCrop"
                android:transitionName="@string/t_movie_list_to_detail"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.6" />

  • 在代码中定义
//第一个页面
val intent = Intent(activity, MovieDetailAct::class.java)
        intent.putExtra("id", mData[position].id)
        //第二个参数:共享元素的view  第三个参数:在xml文件中定义的transitionName
        val optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
                shareView,
                getString(R.string.t_movie_list_to_detail))
        startActivity(intent, optionsCompat.toBundle())


//第二个页面
postponeEnterTransition() //延迟动画
...
...
startPostponedEnterTransition() //开始动画
图片取色

用到了 v7 包中的工具 Palette,需要注意的是由于图片的复杂性,不一定每张图片都能取到相应的颜色,所以一定要做非空判断。

                Palette.from(resource).generate {
                    if (it != null) {
                        val dvs = it.darkVibrantSwatch //暗色有活力的
                        val lvs = it.lightVibrantSwatch //亮色有活力的
                        val dms = it.darkMutedSwatch //暗色柔和的
                        val lms = it.lightMutedSwatch //亮色柔和的
                        var color = when {
                            dvs?.rgb != null -> dvs.rgb
                            lvs?.rgb != null -> lvs.rgb
                            dms?.rgb != null -> dms.rgb
                            lms?.rgb != null -> lms.rgb
                            else -> ContextCompat.getColor(getContext(), R.color.themColor)
                        }
                        window.statusBarColor = color //状态栏
                        movie_detail_coll_head_bg.setBackgroundColor(color)
                        movie_detail_coll_layout.setContentScrimColor(color)
                    }
                }
定义高阶函数
fun setOnItemClickListener(listener: (itemView: View, position: Int, shareView: View) -> Unit) {
        itemClick = listener
    }
 ....
 ....    
 holder?.btn?.setOnClickListener {
            if (itemBtnClick != null)
                itemBtnClick.invoke(it, position)
        }

关于第二个小功能

第二个功能的效果是看到了今日头条的一条广告效果,觉得很有意思,就想自己实现一下。 自己做下来代码量不是很多,但是里面涉及到原理还是值得搞明白的。准备另外写一篇文章记录一下,总的下来有一个感受就是: 数学真是个好东西

最后

总的来说觉得Kotlin还是很好用的,值得深入学习。代码量很少,适合练手,代码以上传到GitHub