成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用Kotlin高效地開發Android App(一)

移動開發
最近我們在做區塊鏈相關的錢包項目,新的App使用全新的技術棧。在Android中我們使用Kotlin+RxJava+Android Architecture Components,在iOS中使用Swift+RxSwift。本文不討論App的架構,只討論項目中所使用到的Kotlin的特性。

[[227035]]
星戰小兵.jpg

背景

最近我們在做區塊鏈相關的錢包項目,新的App使用全新的技術棧。在Android中我們使用Kotlin+RxJava+Android Architecture Components,在iOS中使用Swift+RxSwift。本文不討論App的架構,只討論項目中所使用到的Kotlin的特性。

在Android的App中,可以毫不夸張地說,我們95%以上的代碼使用了Kotlin開發的。由此,很有必要對這一階段使用Kotlin做一個簡單的小結。

使用的Kotlin特性:

一.擴展函數

Kotlin允許開發者在不改變已有類的情況下,為某個類添加新的函數。這個特性叫做擴展函數。

舉一個簡單的例子。如果要關閉一個I/O流,使用Java可能是寫一個工具方法。

 

  1. /**  
  2. * 安全關閉io流  
  3. * @param closeable  
  4. */  
  5. public static void closeQuietly(Closeable closeable) {  
  6. if (closeable != null) {  
  7. try { 
  8.  closeable.close();  
  9. } catch (IOException e) {  
  10. e.printStackTrace();  
  11.  
  12.  

對Kotlin而言,可以對Closeable擴展一個函數closeQuietly()。

 

  1. fun Closeable?.closeQuietly() {  
  2. try {  
  3. this?.close()  
  4. } catch (e: Throwable) {  
  5.  

之后,任何實現了Closeable接口的類,都可以使用它本身的closeQuietly()方法來關閉流。我們不再需要那個工具方法了。

在項目中,我們使用擴展函數對Glide做了封裝,大大簡化了Glide的使用。

 

  1. /**  
  2. * 占位符矩形  
  3. */  
  4. fun ImageView.load(url: String) {  
  5. get(url).placeholder(R.drawable.shape_default_rec_bg)  
  6. .error(R.drawable.shape_default_rec_bg) 
  7.  .into(this)  
  8.  
  9. /**  
  10. * 占位符圓角矩形  
  11. */  
  12. fun ImageView.loadRound(url: String) {  
  13. get(url).placeholder(R.drawable.shape_default_round_bg)  
  14. .error(R.drawable.shape_default_round_bg)  
  15. // .apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0))) 
  16.  .transform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0))  
  17. .into(this)  
  18.  /**  
  19. * 占位符圓形  
  20. */  
  21. fun ImageView.loadCircle(url: Drawable) {  
  22. get(url).placeholder(R.drawable.shape_default_circle_bg)  
  23. .error(R.drawable.shape_default_circle_bg)  
  24. .into(this)  
  25.  fun ImageView.loadCircle(url: String) {  
  26. get(url).placeholder(R.drawable.shape_default_circle_bg)  
  27. .error(R.drawable.shape_default_circle_bg)  
  28. .into(this)  
  29.  
  30. fun ImageView.get(url: String): GlideRequest = GlideApp.with(context).load(url)  
  31. fun ImageView.get(url: Drawable): GlideRequest = GlideApp.with(context).load(url) 

除此之外,我們還很多地方都用到了擴展函數。

我順便更新了我的Kolin的工具類庫,它包括各種utils和各種extension

https://github.com/fengzhizi715/SAF-Kotlin-Utils

二.尾隨閉包

一開始我并不了解這個概念。偶然間我看到我們的小伙伴在使用RxBus時,寫下了這樣的代碼:

  1. RxBus.get().register(LogoutEvent::class.java) { refresh() } 

當時我感覺很疑惑,因為RxBus是我寫的,記得沒有提供這樣的方法啊。點擊register()方法進去看之后,發現register是這樣的:

 

  1. public Disposable register(Class eventType, Consumer onNext) {  
  2. return toObservable(eventType).observeOn(AndroidSchedulers.mainThread()).subscribe(onNext);  

由于使用了Kotlin,該register方法的使用可以簡化成這樣:

 

  1. RxBus.get().register(LogoutEvent::class.java,{  
  2. refresh()  
  3. }) 

由于register()***一個參數是一個方法或者說是一個閉包,可以把方法或者閉包提到最外面。變成項目中看到的樣子:

  1. RxBus.get().register(LogoutEvent::class.java) { refresh() } 

這就是尾隨閉包,可以讓代碼看起來更加簡潔。

三.with的用法

with是將某個對象作為函數的參數,在函數塊內可以通過 this 指代該對象。在函數塊內可以直接調用對象的方法或者屬性。

 

  1. /**  
  2. * Calls the specified function [block] with the given [receiver] as its receiver and returns its result.  
  3. */  
  4. @kotlin.internal.InlineOnly  
  5. public inline fun with(receiver: T, block: T.() -> R): R {  
  6. contract {  
  7. callsInPlace(block, InvocationKind.EXACTLY_ONCE)  
  8.  
  9. return receiver.block()  

在使用with之前的某個Adapter

 

  1. class AppPublisherAdapter : BaseAdapter() {  
  2. override fun getLayoutId(viewType: Int): Int = R.layout.cell_app_publisher  
  3. override fun onBindViewHolderImpl(holder: BaseViewHolder, position: Int,content: BoundAppInfoResponse.AppInfo) {  
  4. holder.itemView.tv_game_name.text = content.name 
  5.  if (content.is_bound) {  
  6. holder.itemView.tv_bound_user_name.text = content.bound_user_name  
  7. holder.itemView.tv_bound_user_name.setTextColor(context.resources.getColor(R.color.color_bound_user_name))  
  8. else {  
  9. holder.itemView.tv_bound_user_name.text = context.getString(R.string.bind_on_account)  
  10. holder.itemView.tv_bound_user_name.setTextColor(context.resources.getColor(R.color.color_bind_on_account))  
  11.  
  12. holder.itemView.iv_game_icon.load(content.logo_url)  
  13.  

使用with之后,該函數塊可以省略"content."

 

  1. class AppPublisherAdapter : BaseAdapter() {  
  2. override fun getLayoutId(viewType: Int): Int = R.layout.cell_app_publisher  
  3. override fun onBindViewHolderImpl(holder: BaseViewHolder, position: Int, content: BoundAppInfoResponse.AppInfo) {  
  4. with(content) {  
  5. holder.itemView.tv_game_name.text = name  
  6. if (is_bound) {  
  7. holder.itemView.tv_bound_user_name.text = bound_user_name  
  8. holder.itemView.tv_bound_user_name.setTextColor(context.color(R.color.color_bound_user_name))  
  9. else {  
  10. holder.itemView.tv_bound_user_name.text = context.string(R.string.bind_on_account)  
  11. holder.itemView.tv_bound_user_name.setTextColor(context.color(R.color.color_bind_on_account))  
  12.  
  13. holder.itemView.iv_game_icon.load(logo_url)  
  14.  
  15.  

四.其他

這部分的內容并不是Kotlin的特性,是我使用Kotlin開發的工具。比如日志框架L以及Retrofit的日志攔截器。這些庫,其實很早就開發了,最近稍微升級了一下功能。

L的github地址:

  • https://github.com/fengzhizi715/SAF-Kotlin-log

Retrofit日志攔截器的github地址:

  • https://github.com/fengzhizi715/saf-logginginterceptor

日志攔截器的效果圖:

使用Kotlin高效地開發Android App(一)
request的效果圖.jpeg

使用Kotlin高效地開發Android App(一)
response的效果圖.jpeg

總結

Kotlin吸收了多種語言的優點,相對于Java有很多激動人心的特性,極大地提高了開發效率。本文介紹的特性也只是滄海一粟。接下來,我會整理更多項目中所使用的Kotlin特性。

BTW,我在寫這篇文章的時候國內***個錢包版本剛剛做完,開始***輪測試。

責任編輯:未麗燕 來源: 簡書
相關推薦

2010-03-03 15:06:52

Android 游戲開

2017-05-22 11:09:53

KotlinAndroid

2021-03-08 07:46:53

Git開源控制系統

2021-08-05 18:34:55

IntelliJ ID高效

2015-09-06 14:50:05

安卓app高效開發

2014-04-08 10:22:29

Android高效開發App

2021-01-18 13:17:04

鴻蒙HarmonyOSAPP

2023-11-28 08:22:05

goroutine語言

2024-01-08 09:00:00

開發DSLKotlin

2022-05-11 09:51:10

云計算公共云

2013-02-18 08:39:15

powershell

2013-05-28 14:39:25

Android開發Android App

2021-01-28 14:53:19

PHP編碼開發

2018-06-20 11:00:06

云應用開發PaaS

2017-05-09 09:36:52

Android App高效顯示位圖

2016-11-23 08:10:16

Android St JRebel調試神器

2019-04-01 14:17:36

kotlin開發Java

2019-09-27 12:44:03

數據建模企業數據存儲

2021-05-31 07:57:00

拼接字符串Java

2021-02-25 22:17:19

開發技術編程
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久国产精品无码网站 | 午夜激情国产 | 久久精品69| 欧美黑人狂野猛交老妇 | 国产精品视频免费看 | 成人免费在线 | 欧美极品一区二区 | 91久久精品 | 日韩成人一区 | 亚洲色图综合 | 亚洲乱码国产乱码精品精98午夜 | 精品国产乱码久久久 | 91精品国产综合久久婷婷香蕉 | 久久精品视频一区二区三区 | 欧美久久久久久久久中文字幕 | 岛国av一区二区三区 | 中文字幕在线精品 | 在线视频h | 夜夜av | 一区二区三区回区在观看免费视频 | 久久久久久久久99精品 | 国产精品96久久久久久 | 超碰av在线| 国产片侵犯亲女视频播放 | 国产精品完整版 | 亚洲欧美成人在线 | 久久99精品久久久久子伦 | 欧美日韩福利 | 亚洲欧美视频 | 一区二区三区免费观看 | 精品欧美一区二区精品久久 | 国产成人99久久亚洲综合精品 | h在线免费观看 | 久久久久久久久久久久久久国产 | 久久亚洲综合 | 激情综合五月 | 欧美精品区 | 四虎影院免费在线 | 成人一级片在线观看 | 一级毛片色一级 | 国产精品视频网站 |