前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Kotlin高效地开发Android App(一)

使用Kotlin高效地开发Android App(一)

作者头像
fengzhizi715
发布2018-08-24 17:28:22
7590
发布2018-08-24 17:28:22
举报

星战小兵.jpg

背景

最近我们在做区块链相关的钱包项目,新的App使用全新的技术栈。在Android中我们使用Kotlin+RxJava+Android Architecture Components,在iOS中使用Swift+RxSwift。本文不讨论App的架构,只讨论项目中所使用到的Kotlin的特性。

在Android的App中,可以毫不夸张地说,我们95%以上的代码使用了Kotlin开发的。由此,很有必要对这一阶段使用Kotlin做一个简单的小结。

使用的Kotlin特性:

一.扩展函数

Kotlin允许开发者在不改变已有类的情况下,为某个类添加新的函数。这个特性叫做扩展函数。

举一个简单的例子。如果要关闭一个I/O流,使用Java可能是写一个工具方法。

    /**
     * 安全关闭io流
     * @param closeable
     */
    public static void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

对Kotlin而言,可以对Closeable扩展一个函数closeQuietly()。

fun Closeable?.closeQuietly() {
    try {
        this?.close()
    } catch (e: Throwable) {
    }
}

之后,任何实现了Closeable接口的类,都可以使用它本身的closeQuietly()方法来关闭流。我们不再需要那个工具方法了。

在项目中,我们使用扩展函数对Glide做了封装,大大简化了Glide的使用。

/**
 * 占位符矩形
 */
fun ImageView.load(url: String) {
    get(url).placeholder(R.drawable.shape_default_rec_bg)
            .error(R.drawable.shape_default_rec_bg)
            .into(this)
}

/**
 * 占位符圆角矩形
 */
fun ImageView.loadRound(url: String) {
    get(url).placeholder(R.drawable.shape_default_round_bg)
            .error(R.drawable.shape_default_round_bg)
//            .apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0)))
            .transform(RoundedCornersTransformation(DisplayUtil.dp2px(context, 6f), 0))
            .into(this)
}

/**
 * 占位符圆形
 */
fun ImageView.loadCircle(url: Drawable) {
    get(url).placeholder(R.drawable.shape_default_circle_bg)
            .error(R.drawable.shape_default_circle_bg)
            .into(this)
}

fun ImageView.loadCircle(url: String) {
    get(url).placeholder(R.drawable.shape_default_circle_bg)
            .error(R.drawable.shape_default_circle_bg)
            .into(this)
}

fun ImageView.get(url: String): GlideRequest<Drawable> = GlideApp.with(context).load(url)
fun ImageView.get(url: Drawable): GlideRequest<Drawable> = GlideApp.with(context).load(url)

除此之外,我们还很多地方都用到了扩展函数。

我顺便更新了我的Kolin的工具类库,它包括各种utils和各种extension

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

二.尾随闭包

一开始我并不了解这个概念。偶然间我看到我们的小伙伴在使用RxBus时,写下了这样的代码:

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

当时我感觉很疑惑,因为RxBus是我写的,记得没有提供这样的方法啊。点击register()方法进去看之后,发现register是这样的:

    public <T> Disposable register(Class<T> eventType, Consumer<T> onNext) {
        return toObservable(eventType).observeOn(AndroidSchedulers.mainThread()).subscribe(onNext);
    }

由于使用了Kotlin,该register方法的使用可以简化成这样:

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

由于register()最后一个参数是一个方法或者说是一个闭包,可以把方法或者闭包提到最外面。变成项目中看到的样子:

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

这就是尾随闭包,可以让代码看起来更加简洁。

三.with的用法

with是将某个对象作为函数的参数,在函数块内可以通过 this 指代该对象。在函数块内可以直接调用对象的方法或者属性。

/**
 * Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return receiver.block()
}

在使用with之前的某个Adapter

class AppPublisherAdapter : BaseAdapter<BoundAppInfoResponse.AppInfo>() {

    override fun getLayoutId(viewType: Int): Int = R.layout.cell_app_publisher

    override fun onBindViewHolderImpl(holder: BaseViewHolder, position: Int,content: BoundAppInfoResponse.AppInfo) {
        holder.itemView.tv_game_name.text = content.name

        if (content.is_bound) {
            holder.itemView.tv_bound_user_name.text = content.bound_user_name
            holder.itemView.tv_bound_user_name.setTextColor(context.resources.getColor(R.color.color_bound_user_name))
        } else {
            holder.itemView.tv_bound_user_name.text = context.getString(R.string.bind_on_account)
            holder.itemView.tv_bound_user_name.setTextColor(context.resources.getColor(R.color.color_bind_on_account))
        }
        holder.itemView.iv_game_icon.load(content.logo_url)
    }
}

使用with之后,该函数块可以省略"content."

class AppPublisherAdapter : BaseAdapter<BoundAppInfoResponse.AppInfo>() {

    override fun getLayoutId(viewType: Int): Int = R.layout.cell_app_publisher

    override fun onBindViewHolderImpl(holder: BaseViewHolder, position: Int, content: BoundAppInfoResponse.AppInfo) {

        with(content) {
            holder.itemView.tv_game_name.text = name

            if (is_bound) {
                holder.itemView.tv_bound_user_name.text = bound_user_name
                holder.itemView.tv_bound_user_name.setTextColor(context.color(R.color.color_bound_user_name))
            } else {
                holder.itemView.tv_bound_user_name.text = context.string(R.string.bind_on_account)
                holder.itemView.tv_bound_user_name.setTextColor(context.color(R.color.color_bind_on_account))
            }
            holder.itemView.iv_game_icon.load(logo_url)
        }
    }
}

四.其他

这部分的内容并不是Kotlin的特性,是我使用Kotlin开发的工具。比如日志框架L以及Retrofit的日志拦截器。这些库,其实很早就开发了,最近稍微升级了一下功能。

L的github地址:

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

Retrofit日志拦截器的github地址:

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

日志拦截器的效果图:

request的效果图.jpeg

response的效果图.jpeg

总结

Kotlin吸收了多种语言的优点,相对于Java有很多激动人心的特性,极大地提高了开发效率。本文介绍的特性也只是沧海一粟。接下来,我会整理更多项目中所使用的Kotlin特性。

BTW,我在写这篇文章的时候国内第一个钱包版本刚刚做完,开始第一轮测试。

该系列的相关文章:

使用Kotlin高效地开发Android App(五)完结篇

使用Kotlin高效地开发Android App(四)

使用Kotlin高效地开发Android App(三)

使用Kotlin高效地开发Android App(二)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.04.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 使用的Kotlin特性:
    • 一.扩展函数
      • 二.尾随闭包
        • 三.with的用法
          • 四.其他
          • 总结
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档