重要知识点

下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。

高级进阶篇——高级UI,自定义View(部分展示)

UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!

  • 面试题部分合集

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

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

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

},
“error_code”:0
}

再来看一下我的数据类:

data class CalentarDayBean(
val reason: String,
val result: CalentarDayResult,
val error_code: Int
)

data class CalentarDayResult(
val data: CalentarDayData
)

data class CalentarDayData(
val date: String,
val weekday: String,
val animalsYear: String,
val suit: String,
val avoid: String,
val yearMonth: String,
val holiday: String,
val lunar: String,
val lunarYear: String,
val desc: String
)

就是如此方便

7、MVP

kotlin的MVP和java原理一模一样我先定义了IBaseModel和IBaseView

IBaseModel

interface IBaseModel {

fun onDestroy()

fun attachView(view: T)
}

IBaseView

interface IBaseView {

fun showLoading()

fun hideLoading()

fun showMessage(message: String)

fun killMyself()
}

然后完成ICalentarContract,这个类似合同类的接口把P和V的所有方法全部写在一起,看起来代码格外清楚

interface ICalentarContract {
/**

  • 对于经常使用的关于UI的方法可以定义到IBaseView中,如显示隐藏进度条,和显示文字消息
    */
    interface View : IBaseView {
    fun showDayCalentarData(calentarDayBean: CalentarDayBean)
    fun showError(errorMsg: String)
    }

/**

  • Model层定义接口,外部只需关心Model返回的数据,无需关心内部细节,如是否使用缓存
    */
    interface Model : IBaseModel<ICalentarContract.View> {
    fun getDayCalentarData(date: String)
    }
    }

然后activity去实现ICalentarContract.View,presenter去实现ICalentarContract.Model

class CalentarDatePresenter : ICalentarContract.Model {

}

class MainActivity : AppCompatActivity(), ICalentarContract.View {

}

so easy~~~ 到这里我们的Demo就完成了,可以尽情玩乐。

项目地址:待上传。。。。。。。。。。。。。

好了,到这里我们基本掌握了Kotlin在安卓中的应用,那么接下来就需要去学习一下kotlin设计模式以及一些进阶知识~

进阶

一、Kotlin设计模式
本文只列出几个常用的设计模式

1、观察者模式( observer pattern )

Example

interface TextChangedListener {
fun onTextChanged(newText: String)
}

class PrintingTextChangedListener : TextChangedListener {
override fun onTextChanged(newText: String) = println(“Text is changed to: $newText”)
}

class TextView {

var listener: TextChangedListener? = null

var text: String by Delegates.observable(“”) { prop, old, new ->
listener?.onTextChanged(new)
}
}

Usage

val textView = TextView()
textView.listener = PrintingTextChangedListener()
textView.text = “Lorem ipsum”
textView.text = “dolor sit amet”

Output

Text is changed to: Lorem ipsum
Text is changed to: dolor sit amet

2、策略模式( strategy pattern )

Example

class Printer(val stringFormatterStrategy: (String) -> String) {
fun printString(string: String) = println(stringFormatterStrategy.invoke(string))
}

val lowerCaseFormatter: (String) -> String = { it.toLowerCase() }

val upperCaseFormatter = { it: String -> it.toUpperCase() }

Usage

val lowerCasePrinter = Printer(lowerCaseFormatter)
lowerCasePrinter.printString(“LOREM ipsum DOLOR sit amet”)

val upperCasePrinter = Printer(upperCaseFormatter)
upperCasePrinter.printString(“LOREM ipsum DOLOR sit amet”)

val prefixPrinter = Printer({ "Prefix: " + it })
prefixPrinter.printString(“LOREM ipsum DOLOR sit amet”)

Output

lorem ipsum dolor sit amet
LOREM IPSUM DOLOR SIT AMET
Prefix: LOREM ipsum DOLOR sit amet

3、单例模式(singleton pattern)

Example

class Singletone private constructor() {
init {
println(“Initializing with object: $this”)
}

companion object {
val getInstance =SingletonHolder.holder
}

private object SingletonHolder {
val holder = Singletone()
}

fun print() = println(“Printing with object: $this”)
}

Usage

Singletone.getInstance.print()
Singletone.getInstance.print()

Output

Initializing with object: advance.Singletone@266474c2
Printing with object: advance.Singletone@266474c2
Printing with object: advance.Singletone@266474c2

4、工厂模式(Factory Method)

Example

interface Currency {
val code: String
}

class Euro(override val code: String = “EUR”) : Currency
class UnitedStatesDollar(override val code: String = “USD”) : Currency

enum class Country {
UnitedStates, Spain, UK, Greece
}

class CurrencyFactory {
fun currencyForCountry(country: Country): Currency? {
when (country) {
Country.Spain, Country.Greece -> return Euro()
Country.UnitedStates -> return UnitedStatesDollar()
else -> return null
}
}
}

Usage

val noCurrencyCode = “No Currency Code Available”

val greeceCode = CurrencyFactory().currencyForCountry(Country.Greece)?.code() ?: noCurrencyCode
println(“Greece currency: $greeceCode”)

val usCode = CurrencyFactory().currencyForCountry(Country.UnitedStates)?.code() ?: noCurrencyCode
println(“US currency: $usCode”)

val ukCode = CurrencyFactory().currencyForCountry(Country.UK)?.code() ?: noCurrencyCode
println(“UK currency: $ukCode”)

Output

Greece currency: EUR
US currency: USD
UK currency: No Currency Code Available

5、代理模式(Protection Proxy)

Example

interface File {
fun read(name: String)
}

class NormalFile : File {
override fun read(name: String) = println(“Reading file: $name”)
}

//Proxy:
class SecuredFile : File {
val normalFile = NormalFile()
var password: String = “”

override fun read(name: String) {
if (password == “secret”) {
println(“Password is correct: $password”)
normalFile.read(name)
} else {
println(“Incorrect password. Access denied!”)
}
}
}

Usage

val securedFile = SecuredFile()
securedFile.read(“readme.md”)

securedFile.password = “secret”
securedFile.read(“readme.md”)

Output

Incorrect password. Access denied!
Password is correct: secret
Reading file: readme.md

6、建造者模式(builder pattern)

Example

// Let’s assume that Dialog class is provided by external library.
// We have only access to Dialog public interface which cannot be changed.

class Dialog() {

fun showTitle() = println(“showing title”)

fun setTitle(text: String) = println(“setting title text $text”)

fun setTitleColor(color: String) = println(“setting title color $color”)

fun showMessage() = println(“showing message”)

fun setMessage(text: String) = println(“setting message $text”)

fun setMessageColor(color: String) = println(“setting message color $color”)

fun showImage(bitmapBytes: ByteArray) = println(“showing image with size ${bitmapBytes.size}”)

fun show() = println(“showing dialog $this”)
}

//Builder:
class DialogBuilder() {
constructor(init: DialogBuilder.() -> Unit) : this() {
init()
}

private var titleHolder: TextView? = null
private var messageHolder: TextView? = null
private var imageHolder: File? = null

fun title(init: TextView.() -> Unit) {
titleHolder = TextView().apply { init() }
}

fun message(init: TextView.() -> Unit) {
messageHolder = TextView().apply { init() }
}

fun image(init: () -> File) {
imageHolder = init()
}

fun build(): Dialog {
val dialog = Dialog()

titleHolder?.apply {
dialog.setTitle(text)
dialog.setTitleColor(color)
dialog.showTitle()
}

messageHolder?.apply {
dialog.setMessage(text)
dialog.setMessageColor(color)
dialog.showMessage()
}

imageHolder?.apply {
dialog.showImage(readBytes())
}

return dialog
}

class TextView {
var text: String = “”
var color: String = “#00000”
}
}

Usage

//Function that creates dialog builder and builds Dialog
fun dialog(init: DialogBuilder.() -> Unit): Dialog {
return DialogBuilder(init).build()
}

val dialog: Dialog = dialog {
title {
text = “Dialog Title”
}
message {
text = “Dialog Message”
color = “#333333”
}
image {
File.createTempFile(“image”, “jpg”)
}
}

dialog.show()

Output

setting title text Dialog Title
setting title color #00000
showing title
setting message Dialog Message
setting message color #333333
showing message
showing image with size 0
showing dialog Dialog@5f184fc6

2、相关书籍

个人认为还是需要找一本书籍好好地阅读一遍,一下提供了相关书籍可以选择适合自己的。

NO.1

《Kotlin for Android Developers》

Kotlin是编写Android应用程序的新官方语言,多亏了这本书,你很快就能写出代码。直奔主题,实用和完整的例子,它将在开发Android应用程序的同时展示你的语言。学习Kotlin并开始使用这个强大而现代的语言再次享受Android开发。

NO.2

《Kotlin开发快速入门与实战》

学习本书之前不需要具备任何的计算机专业背景,任何有志于APP开发的读者都能利用本书从头学起。

资深软件开发工程师根据Kotlin最新版本撰写,系统讲解Kotlin开发技巧和项目实战。全书共分为7章,内容层次清晰,难度循序渐进。希望通过阅读本书,能够让你成为一个全栈工程师。

NO.3

《疯狂Kotlin讲义》

本书尤其适合从Java转Kotlin的读者,对于没有Java功底的读者,可忽略“对比”部分,直接学习本书也可掌握Kotlin编程。

本书对Kotlin的解读十分系统、全面,超过Kotlin官方文档本身覆盖的内容。本书很多地方都会结合Java字节码进行深入解读,比如对Kotlin扩展的解读,对Kotlin主、次构造器的解读,这种解读目的不止于教会读者简单地掌握Kotlin的用法,而是力求让读者深入理解Kotlin,且更好地理解Java。

NO.4

《Kotlin实战》

本书主要面向有一定Java 经验的开发者。

本书将从语言的基本特性开始,逐渐覆盖其更多的高级特性,尤其注重讲解如何将 Koltin 集成到已有 Java 工程实践及其背后的原理。本书分为两个部分。第一部分讲解如何开始使用 Kotlin 现有的库和API,包括基本语法、扩展函数和扩展属性、数据类和伴生对象、lambda 表达式,以及数据类型系统(着重讲解了可空性和集合的概念)。第二部分教你如何使用 Kotlin 构建自己的 API,以及一些深层次特性——约定和委托属性、高阶函数、泛型、注解和反射,以及领域特定语言的构建。

本书适合广大移动开发者及入门学习者,尤其是紧跟主流趋势的前沿探索者。

NO.5

《揭秘Kotlin编程原理》

本书深入介绍Kotlin面向对象设计的语法特性及其背后的实现方式。

在本书中,读者不仅能清晰地了解Kotlin的语法、高级特性,还能真正地掌握Kotlin背后的实现机制和设计哲学,形成对Kotlin语言既直观、又深刻的认识——在此基础上,读者能准确、快速地上手实践,大大提升自己的移动开发能力。

Kotlin的这些特性和实现机制,可以帮助开发者扫清开发道路上的一些障碍,让开发变得更加简单!本书是一本值得拥有,能切实帮助读者加薪提职的好书!

项目

学习一门语言最快的方式就是看其如何在实际项目中运用,有了上面的基础和进阶,下面我们看一些开源项目:

1.Kotlin-for-Android-Developers(★1676)

介绍:这个项目其实是Kotlin-for-Android-Developers这本书的配套代码,如果你是kotlin的初学者,那么这绝对是你学习kotlin的不二之选。项目通过一个天气的例子很好的展示了kotlin带来的强大功能,比如网络数据的请求,数据的缓存设计,数据库的操作,各种扩展函数的妙用等等。

地址:https://github.com/antoniolg/Kotlin-for-Android-Developers

2.Bandhook-Kotlin (★1494)

介绍:Kotlin版本的音乐播放器,数据来源于LastFm。

地址:https://github.com/antoniolg/Bandhook-Kotlin

3.GankClient-Kotlin (★1216)

介绍:gank.io kotlin实现的干货集中营Android客户端,风格采用了Material Design。

地址:https://github.com/githubwing/GankClient-Kotlin

4.PoiShuhui-Kotlin(★897)

介绍:一个用Kotlin写的简单漫画APP。

地址:https://github.com/wuapnjie/PoiShuhui-Kotlin

5.Eyepetizer-in-Kotlin(★1167)

介绍:Kotlin版本的Eyepetizer客户端

地址:https://github.com/LRH1993/Eyepetizer-in-Kotlin

6.Tucao(★792)

介绍:Kotlin版本的吐槽客户端

地址:https://github.com/blackbbc/Tucao

资源

一、重要资源
Kotlin 官网

https://kotlinlang.org/docs/reference/

Kotlin 官方网站是学习 Kotlin 好去处。在参考部分,你可以找到该语言的所有概念和功能的深入解析文档。在教程部分有关于设置工作环境并使用编译器的实用分步指南。

这里还有个 Kotlin 编译器,是一个浏览器 APP,你可以在上面尝试使用这门语言。它能加载许多示例,包括 Koans 课程 — 这是目前熟悉 Kotlin 语法的最好方式。

Kotlin 官博

https://blog.jetbrains.com/kotlin/

Kotlin 的官方博客由 JetBrains 的一位作者负责。你可以在这里找到所有与 Kotlin 相关的新闻、更新、教程、使用技巧等的内容。

在 Android 上开始使用 Kotlin

https://developer.android.com/kotlin/get-started.html

一篇很牛叉的文章,向我们展示了如何使用 Kotlin 编写和运行 Android 应用程序的测试

从 Java 到 Kotlin

https://github.com/MindorksOpenSource/from-java-to-kotlin

最后的最后

对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的

最后,互联网不存在所谓的寒冬,只是你没有努力罢了!

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

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

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

ndroid 应用程序的测试

从 Java 到 Kotlin

https://github.com/MindorksOpenSource/from-java-to-kotlin

最后的最后

对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的

[外链图片转存中…(img-faLEaDfl-1714886431293)]

最后,互联网不存在所谓的寒冬,只是你没有努力罢了!

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

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

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

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐