發表文章

Kotlin: View Binding

 一般情況下的UI component :Button,textview...,使用前須透過 : (最新方法:) developer - View Binding https://developer.android.com/topic/libraries/view-binding (舊式,已可以全部改用view binding.) ButterKnife https://github.com/JakeWharton/butterknife (原始方法,若程式元件不多,可直接用這方法取得ui元件) findViewById https://developer.android.com/reference/android/view/View#findViewById(int) < Button     android: id= "@+id/button_submit"     android: src= "@drawable/image1"     android: layout_width= "wrap_content"     android: layout_height= "wrap_content" /> var btn_submit = findViewById(R.id.button_submit) as Button 或是: private lateinit var textView : TextView //in onCreate() textView = findViewById(R.id.textView) 這樣的宣告才可使用,否則未宣告就使用會導致直接app crash. 若是有超過10個以上的UI元件,程式碼就變成非常繁瑣,有一大部分都在寫這重複的code,所以就有view-binding的出現. ( In most cases, view binding replaces  findViewById . ) ---- View-Binding: https://developer.android.com/topic/libraries/view-binding  you need to use the below in your app gradle:

Kotlin (5): Coroutines

圖片
學習Kotlin最重要的項目,就是Coroutines. 這可是之前Java所沒有的,Java的Thread 不好用,這個coroutines才是適合非同步領域的東西. Coroutines 這個單字是由兩個英文單字合併而成的,分別是 cooperation + routine.用來做非同步和非阻塞任務.意思就是要各個子任務程協作運行的意思,所以明白了它被創造出來是要解決異步問題的。 Kotlin的協程完美復刻了Go語言的協程設計模式( 作用域/channel/select), 將作用域用對象分類; 可以更好地控制作用域生命週期; await模式(類似JavaScript的異步任務解決方案) Kotlin參考RxJava響應式框架創造出Flow 使用協程開始就不需要考慮線程的問題, 只需要在不同場景使用不同的調度器就好 github:  https://github.com/Kotlin/kotlinx.coroutines kotlin home:  https://kotlinlang.org reference:  https://juejin.cn/post/6987724340775108622 仔細看完文件後,發現coroutines並不是那麼簡單,反而使用方法有點複雜. ---- dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:+' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:+' } ----- coroutine 典型用法, 啟動協程: 啟動一個新的協程, 常用的主要有以下幾種方式:它們被稱為coroutine builders.  launch async (可從Coroutine 返回值) runBlocking ---- launch– Launches new coroutine without blocking current thread and returns a reference to the coroutine as a Job. The coroutine is canceled when the resu

Retrofit in Android

圖片
現在Retrofit 超重要的,以前都使用原始的okhttp 自己轉換json. 但業界作法是用Retrofit, 所以這技能是一定要會的,所以今天我就親自來實作看看. 什麼是Retrofit ? Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services.  It makes it relatively easy to retrieve and upload JSON via a REST based webservice.  In Retrofit you configure which converter is used for the data serialization.   Typically for JSON you use GSon, but you can add custom converters to process XML or other protocols.  Retrofit uses the OkHttp library for HTTP requests ---- Retrofit vs  Volley? Retrofit is a purely abstract layer above okhttp used for network calls and it takes away the boiler plate required for writing those lengthy AsynTasks. So, Retrofit is just another library like Volley, EasyNetwork but it has support for integrating with RxAndroid as well as live data classes from Android Architecture. Retrofit is not related to Java programming.. It is related to Android concepts .. Retrofit is a REST Client for Jav

Kotlin (4): Getters and Setters

Properties in Kotlin var author: String = "Steven" This is the same as defining the following get() and set() methods: (example 1:) var author: String = " Steven "     get() {         return field     }     set(value) {         field = value     } --- (example 2:) class Rectangle(val width: Int, val height: Int) {     val area: Int // property type is optional since it can be inferred from the getter's return type         get() = this.width * this.height } --- (example 3:) var stringRepresentation: String     get() = this.toString()     set(value) {         setDataFromString(value)      } ---- Backing fields: Every property we define is backed by a field that can only be accessed within its   get()   and   set()   methods using   the special  field  keyword . (example:) var counter = 0      set(value) {         if (value >= 0) field = value     } or var rating: Int = 5 get () { if (field < 5 ) { print( "Warning: this is a