自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

[[227035]]
星戰(zhàn)小兵.jpg

背景

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

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

使用的Kotlin特性:

一.擴展函數(shù)

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

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

 

  1. /**  
  2. * 安全關(guān)閉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擴展一個函數(shù)closeQuietly()。

 

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

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

在項目中,我們使用擴展函數(shù)對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) 

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

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

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

二.尾隨閉包

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

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

當時我感覺很疑惑,因為RxBus是我寫的,記得沒有提供這樣的方法啊。點擊register()方法進去看之后,發(fā)現(xiàn)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()***一個參數(shù)是一個方法或者說是一個閉包,可以把方法或者閉包提到最外面。變成項目中看到的樣子:

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

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

三.with的用法

with是將某個對象作為函數(shù)的參數(shù),在函數(shù)塊內(nèi)可以通過 this 指代該對象。在函數(shù)塊內(nèi)可以直接調(diào)用對象的方法或者屬性。

 

  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之后,該函數(shù)塊可以省略"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.  

四.其他

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

L的github地址:

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

Retrofit日志攔截器的github地址:

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

日志攔截器的效果圖:

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

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

總結(jié)

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

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

責(zé)任編輯:未麗燕 來源: 簡書
相關(guān)推薦

2017-05-22 11:09:53

KotlinAndroid

2010-03-03 15:06:52

Android 游戲開

2021-03-08 07:46:53

Git開源控制系統(tǒng)

2014-04-08 10:22:29

Android高效開發(fā)App

2021-08-05 18:34:55

IntelliJ ID高效

2015-09-06 14:50:05

安卓app高效開發(fā)

2023-11-28 08:22:05

goroutine語言

2021-01-18 13:17:04

鴻蒙HarmonyOSAPP

2024-01-08 09:00:00

開發(fā)DSLKotlin

2013-05-28 14:39:25

Android開發(fā)Android App

2022-05-11 09:51:10

云計算公共云

2021-01-28 14:53:19

PHP編碼開發(fā)

2013-02-18 08:39:15

powershell

2016-11-23 08:10:16

Android St JRebel調(diào)試神器

2017-05-09 09:36:52

Android App高效顯示位圖

2018-06-20 11:00:06

云應(yīng)用開發(fā)PaaS

2019-04-01 14:17:36

kotlin開發(fā)Java

2019-09-27 12:44:03

數(shù)據(jù)建模企業(yè)數(shù)據(jù)存儲

2021-05-31 07:57:00

拼接字符串Java

2021-02-25 22:17:19

開發(fā)技術(shù)編程
點贊
收藏

51CTO技術(shù)棧公眾號