做为Android开发者,对于谷歌大大力推的Kotlin确定是要会用的啦。 最近就使用了豆瓣电影的部分接口撸了一个小Demo来学习。(因为电脑比较渣,开了模拟器录屏好卡。其实在手机上仍是很流畅的。哈哈哈)java
遵循MD
风格,采用MVP
架构,使用了Kotlin+Retrofit+RxKotlin
开发。如今主要有两个功能:android
@GET(BaseURL.HOTSCREEN)
fun getHotScreenList(@QueryMap par: HashMap<String, String>): Observable<HotScreenResult>
复制代码
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层调用git
if (view == null || module == null)
return
view?.showLoading()
module?.loadHotScreenData(page, {
view!!.notifyList(it)
view!!.dismissLoading()
}, {
it.printStackTrace()
view!!.dismissLoading()
view!!.showTipMessage(1, "网络异常")})
复制代码
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
,须要注意的是因为图片的复杂性,不必定每张图片都能取到相应的颜色,因此必定要作非空判断。github
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)
}
复制代码
第二个功能的效果是看到了今日头条的一条广告效果,以为颇有意思,就想本身实现一下。 本身作下来代码量不是不少,可是里面涉及到原理仍是值得搞明白的。准备另外写一篇文章记录一下,总的下来有一个感觉就是: 数学真是个好东西。api
总的来讲以为Kotlin仍是很好用的,值得深刻学习。代码量不多,适合练手,代码以上传到GitHub。bash