标题之所有没有使用优秀, 优质等华丽的辞藻修饰, 是因为这个项目是本人在学习 Kotlin 过程中用来练手的, 不敢在给各位前辈和大神面前班门弄斧, 自取其辱, 况且 GitHub 上的玩 Android 客户端已经有很多个版本了, 我也只能望其项背
自 Google 在 2017 的 I/O 大会上宣布 Android 支持 Kotlin, 到现在 Kotlin 语言已受到大批 Android 开发者的追捧, 许多大厂已经使用纯 Kotlin 进行 Android 开发
2018 的 I/O 大会又发布可跨平台的 UI 框架 Flutter , 支持在 Android 和 iOS 平台运行
这还没完没了了...
大势所趋吧, 不得不学...
项目简介
上图, 把四张拼一起了
基于 Jetpack(包含 livedata,lifecycle,paging,room) 组件采用 MVVM 架构, Jetpack 组件的使用方法在这里不做说明
组件化开发, module 之间跳转使用阿里的 ARouter 路由框架
数据来源于 hongyang 玩 Android 开放 API http://www.wanandroid.com/blog/show/2 , 聚集了鸿洋, 郭霖等诸多大神的公众号文章和高质量的开源项目推荐介绍
你不妨把这个 App 当做你的的掌上开源社区使用
项目使用到的开源库
kolint 工具库 anko https://github.com/Kotlin/anko
网络请求 okhttp https://github.com/square/okhttp
网络请求 Retrofit https://github.com/square/retrofit
异步框架 Rxjava2 https://github.com/ReactiveX/RxJava
依赖注入 Dagger2 https://github.com/google/dagger
阿里路由框架 ARouter https://github.com/alibaba/ARouter
图片加载 Glide https://github.com/bumptech/glide
日志打印 KLog https://github.com/ZhaoKaiQiang/KLog
可翻页的 RecyclerView https://github.com/GCSSloop/pager-layoutmanager
侧滑删除 SwipeDelMenuLayout https://github.com/mcxtzhang/SwipeDelMenuLayout
流式布局 FlowLayout
轮播图控件 banner https://github.com/youth5201314/banner
项目架构解析
项目分为四个 module
App,App 的壳
module_base, 公共库
module_user,module_wan, 业务模块
既然是组件化开发, 那各个业务模块肯定可以单独运行
- // 根据变量 isModule 判断是作为依赖 library 还是单独的 application
- if (isModule) {
- apply plugin: 'com.android.library'
- } else {
- apply plugin: 'com.android.application'
- }
- sourceSets {
- main {
- // 根据变量 isModule 使用不同文件夹的代码
- if (isModule) {
- manifest.srcFile 'src/main/AndroidManifest.xml'
- java {
- // 去除指定文件夹代码
- exclude '*module'
- }
- } else {
- // 这里面只用一个 Activity, 重当壳 Activity
- java.srcDirs 'src/main/java', 'src/main/module/java'
- manifest.srcFile 'src/main/module/AndroidManifest.xml'
- }
- }
- }
- App
它是 App 的壳, 里面只有一个 MainActivity, 它负责整合所需要的业务模块
- dependencies {
- implementation fileTree(include: ['*.jar'], dir: 'libs')
- implementation project(':module_base')
- kapt kaptLibs
- if (isModule) {
- // 依赖业务 module
- implementation project(':module_user')
- implementation project(':module_wan')
- }
- }
- module_base
作为公共库, 存放 base 和公用代码, 其它的 module 都要依赖它
module_wan 和 module_user
主要的业务
Kotlin 的一些特性在项目中的运用
扩展函数
- // 给 ImageView 添加一个扩展函数, 就当做 ImageView 有了一个 load 方法
- //ImageView 作为接受者类型, 只有 ImageView 和其子类能调用该方法
- fun ImageView.load(url: Any) {
- //this 代表接受这对象
- Glide.with(this).load(url).into(this)
- }
- var imageView: ImageView = ...
- // 直接使用 ImageView 对象调用 load 方法
- imageView.load(url)
以前使用工具类必须要传一个类型为 ImageView 的参数, 现在直接用 ImageView 调用扩展方法是不是简单了很多
空判断
- override fun setTitle(title: CharSequence?) { // 这个? 表示该变量可以为 null
- //? 表示如果 toolbar 不为空才执行后面的代码, 给其属性 title 赋值
- toolbar?.title = title
- }
想想以前代码里面到处都是 if(... != null) 类似的模板代码都阵痛
函数默认参数
- fun <VH : RecyclerView.ViewHolder, A : Adapter<VH>> RecyclerView.init(adapter: A,
- //layoutManager 有一个默认值 LinearLayoutManager(context)
- layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(context)) {
- this.layoutManager = layoutManager
- this.adapter = adapter
- }
- // 只传了一个参数, layoutManager 使用默认值
- recyclerView.init(adapter)
- // 传了两个参数
- flowLayout.init(flowAdapter, FlowLayoutManager())
如果函数有多个参数默认的时候, 我们只想某几个参数使用其默认值, 为了不引起混乱, 我们可以根据参数名传参, 随便写个 Demo 说明一下
- fun main() {
- // 调用函数, 如果不写 c = 默认是 b =
- test(1, c = 10)
- }
- fun test(a: Int, b: Int = 2, c: Int = 3, d: Int = 4) {
- ...
- }
函数式编程
一个函数作为另一个函数的参数传递
- fun <D, T : HttpResult<D>> Observable<T>.subscribeBy(onResponse: (D?) -> Unit, onFailure: (String) -> Unit) =
- subscribe({
- if (it.errorCode == 0) {
- // 函数调用
- onResponse(it.data)
- } else {
- // 函数调用
- onFailure(it.errorMsg)
- }
- }, {
- onFailure(it.message!!)
- })
onResponse 参数接收一个参数类型为 D, 无返回值的方法; onFailure 参数接收一个参数类型为 String, 无返回值的方法. 如果定义一个函数无返回值, 那么它的返回值就是 Unit, 不要问我为什么, 这就是语法, 约定俗成
- API.getKnowledgeArticle(page - 1, typeId).io_main().subscribeBy(
- { onResponse(it?.datas) }, // 第一个参数, it 就是类型为 D 的参数
- { onResponse(null)
- Log.d(TAG,it)
- } // 第二个参数, it 就是类型为 String 的参数
- ).add()
是不是很懵逼, 因为你可能不懂 lambda 表达式的使用
关于 Kotlin 的特性还用很多, 例如 when 表达式, 属性委托, 字符串模板, 类型自动转换, 协程等等, 这里就不一一讲解了, 讲不完的, 现在不熬夜
进阶之路
因每个人学习方法不同, 仅供参考
先在 Kotlin 语言中文站 https://www.kotlincn.net/docs/reference/ 跟着教程敲一遍, 语法你总得先基本掌握吧
多看几个 star 很多的 Kotlin 开源项目, 最好完全敲一遍, 里面还会很有很多你在官网上可能没看到的东西
大神们的代码也会让你有很多意想不到的收获, 起码告诉你, 你的代码 Low 爆了, 会让你有种从此回家带孩子吧的冲动
项目中使用到了很多开源库, 在此向所有对开源做出过贡献的各位大神表示感谢, 正是有你们的开源共享, 我们这些菜鸟的技术才能有所提升, 也是你们用最美的语言, 推动了科技的进步
附上本项目地址 GitHub 传送门 https://github.com/haoshiy/kotlin_wanandroid
Apk 玩 Android 下载地址 https://haoshi.co/wanandroid.apk
扫码下载
End
来源: http://www.jianshu.com/p/a05b10976b08