做爲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