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

.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 = GlideApp.with(context).load(url)
fun ImageView.get(url: Drawable): GlideRequest = 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 Disposable register(Class eventType, Consumer 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

日志拦截器的效果图:

最后

都说三年是程序员的一个坎,能否晋升或者提高自己的核心竞争力,这几年就十分关键。

技术发展的这么快,从哪些方面开始学习,才能达到高级工程师水平,最后进阶到Android架构师/技术专家?我总结了这 5大块;

我搜集整理过这几年阿里,以及腾讯,字节跳动,华为,小米等公司的面试题,把面试的要求和技术点梳理成一份大而全的“ Android架构师”面试 Xmind(实际上比预期多花了不少精力),包含知识脉络 + 分支细节。

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

2021年虽然路途坎坷,都在说Android要没落,但是,不要慌,做自己的计划,学自己的习,竞争无处不在,每个行业都是如此。相信自己,没有做不到的,只有想不到的。祝大家2021年万事大吉。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值