国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Kotlin開發(fā)的一些實(shí)用小技巧總結(jié)

2019-12-12 01:53:59
字體:
供稿:網(wǎng)友

前言

隨著Google I/O大會(huì)的召開,Google宣布將支持Kotlin作為Android的開發(fā)語言,最近關(guān)于Kotlin的文章、介紹就異常的活躍。

本文主要給大家介紹了關(guān)于Kotlin開發(fā)的一些實(shí)用小技巧,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。

1.Lazy Loading(懶加載)

延遲加載有幾個(gè)好處。延遲加載能讓程序啟動(dòng)時(shí)間更快,因?yàn)榧虞d被推遲到訪問變量時(shí)。 這在使用 Kotlin 的 Android 應(yīng)用程序而不是服務(wù)器應(yīng)用程序中特別有用。對(duì)于 Android 應(yīng)用,我們自然希望減少應(yīng)用啟動(dòng)時(shí)間,以便用戶更快地看到應(yīng)用內(nèi)容,而不是等待初始加載屏幕。

懶加載也是更有效率的內(nèi)存,因?yàn)槲覀冎恍枰{(diào)用資源才能將資源加載到內(nèi)存中。例如:

val gankApi: GankApi by lazy { val retrofit: Retrofit = Retrofit.Builder()  .baseUrl(API_URL)  .addConverterFactory(MoshiConverterFactory.create())  .build() retrofit.create(GankApi::class.java)}

如果用戶從沒有調(diào)用 GankApi ,則永遠(yuǎn)不會(huì)加載。因此也不會(huì)占用所需資源。

當(dāng)然懶加載也能較好的用于封裝初始化:

val name: String by lazy { Log.d(TAG, "executed only first time") "Double Thunder"}

如果你不擔(dān)心多線程問題或者想提高更多的性能,你也可以使用

lazy(LazyThreadSafeMode.NONE){ ... }

2. 自定義 Getters/Setters

Kotlin 會(huì)自動(dòng)的使用 getter/setter 模型,但也有一些情況(倒如 Json)我們需要用自定制 getter 和 setter。例如:

@ParseClassName("Book")class Book : ParseObject() { // getString() and put() are methods that come from ParseObject var name: String get() = getString("name") set(value) = put("name", value) var author: String get() = getString("author") set(value) = put("author", value)}

3. Lambdas

button.setOnClickListener { view -> startDetailActivity()}toolbar.setOnLongClickListener {  showContextMenu() true}

4.Data Classes(數(shù)據(jù)類)

數(shù)據(jù)類是一個(gè)簡(jiǎn)單版的 Class,它自動(dòng)添加了包括 equals(), hashCode(), copy(), 和 toString() 方法。將數(shù)據(jù)與業(yè)務(wù)邏輯分開。

data class User(val name: String, val age: Int)

如果使用Gson解析Json的數(shù)據(jù)類,則可以使用默認(rèn)值構(gòu)造函數(shù):

// Example with Gson's @SerializedName annotationdata class User( @SerializedName("name") val name: String = "", @SerializedName("age") val age: Int = 0)

5. 集合過濾

val users = api.getUsers()// we only want to show the active users in one listval activeUsersNames = items.filter {  it.active // the "it" variable is the parameter for single parameter lamdba functions}adapter.setUsers(activeUsers)

6. Object Expressions(對(duì)象表達(dá)式)

Object Expressions 允許定義單例。例如:

package com.savvyapps.example.utilimport android.os.Handlerimport android.os.Looper// notice that this is object instead of classobject ThreadUtil { fun onMainThread(runnable: Runnable) { val mainHandler = Handler(Looper.getMainLooper()) mainHandler.post(runnable) }}

ThreadUtil 則可以直接調(diào)用靜態(tài)類方法:

ThreadUtil.onMainThread(runnable)

以類似的方式,我們創(chuàng)建對(duì)象而不是匿名內(nèi)部類:

viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrollStateChanged(state: Int) {} override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {} override fun onPageSelected(position: Int) {  bindUser(position) }});

這兩個(gè)都基本上是相同的事情 - 創(chuàng)建一個(gè)類作為聲明對(duì)象的單個(gè)實(shí)例。

7. Companion Object(伴生對(duì)象)

Kotlin 是沒有靜態(tài)變量與方法的。相對(duì)應(yīng)的,可以使用伴生對(duì)象。伴生對(duì)象允許定義的常量和方法,類似于 Java 中的 static。有了它,你可以遵循 newInstance 的片段模式。

class ViewUserActivity : AppCompatActivity() { companion object {  const val KEY_USER = "user"  fun intent(context: Context, user: User): Intent {   val intent = Intent(context, ViewUserActivity::class.java)   intent.putExtra(KEY_USER, user)   return intent  } }  override fun onCreate(savedInstanceState: Bundle?) {  super.onCreate(savedInstanceState)  setContentView(R.layout.activity_cooking)    val user = intent.getParcelableExtra<User>(KEY_USER)  //... }}

我們熟悉的使用:

val intent = ViewUserActivity.intent(context, user)startActivity(intent)

8.Global Constants(全局常量)

Kotlin 允許跨越整個(gè)應(yīng)用的全局常量。通常,常量應(yīng)盡可能減少其范圍,但是全局都需要這個(gè)常量時(shí),這是一個(gè)很好的方式。

const val PRESENTATION_MODE_PRESENTING = "presenting"const val PRESENTATION_MODE_EDITING = "editing"

9.Optional Parameters(可選參數(shù))

可選參數(shù)使得方法調(diào)用更加靈活,而不必傳遞 null 或默認(rèn)值。 例如:這在定義動(dòng)畫時(shí):

fun View.fadeOut(duration: Long = 500): ViewPropertyAnimator { return animate()   .alpha(0.0f)   .setDuration(duration)}icon.fadeOut() // fade out with default time (500)icon.fadeOut(1000) // fade out with custom time

10. Extensions(擴(kuò)展屬性)

例如:在 Activity 調(diào)用鍵盤的隱藏

fun Activity.hideKeyboard(): Boolean { val view = currentFocus view?.let {  val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE)     as InputMethodManager  return inputMethodManager.hideSoftInputFromWindow(view.windowToken,    InputMethodManager.HIDE_NOT_ALWAYS) } return false}

推薦一個(gè)收集 Extensions 的網(wǎng)站 。 kotlinextensions.com

11. lateinit

對(duì)于 Null 的檢查是 Kotlin 的特點(diǎn)之一,所以在數(shù)據(jù)定義時(shí),初始化數(shù)據(jù)。但有一些在 Android 中某些屬性需要在 onCreate() 方法中初始化。

private lateinit var mAdapter: RecyclerAdapter<Transaction>override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mAdapter = RecyclerAdapter(R.layout.item_transaction)}

如果是基礎(chǔ)數(shù)據(jù)類型:

var count: Int by Delegates.notNull<Int>()var name:String by Delegate()

如果使用 Butter Knife:

@BindView(R.id.toolbar) lateinit var toolbar: Toolbaroverride fun onCreate(savedInstanceState: Bundle?) {  super.onCreate(savedInstanceState)  setContentView(R.layout.activity_main)  ButterKnife.bind(this)  // you can now reference toolbar with no problems!  toolbar.setTitle("Hello There")}

12. Safe Typecasting(安全轉(zhuǎn)換)

在 Android 中需要安全類型轉(zhuǎn)換。當(dāng)您首先在 Kotlin 中進(jìn)行類型轉(zhuǎn)換時(shí),您可以這樣實(shí)現(xiàn):

var feedFragment: FeedFragment? = supportFragmentManager .findFragmentByTag(TAG_FEED_FRAGMENT) as FeedFragment

但實(shí)際上這樣只能導(dǎo)致崩潰。當(dāng)調(diào)用『as』時(shí),它將進(jìn)行對(duì)象轉(zhuǎn)換,但如果轉(zhuǎn)換的對(duì)象為『null』時(shí),則會(huì)報(bào)錯(cuò)。正確的使用方式應(yīng)該是用『as?』:

var feedFragment: FeedFragment? = supportFragmentManager .findFragmentByTag(TAG_FEED_FRAGMENT) as? FeedFragmentif (feedFragment == null) { feedFragment = FeedFragment.newInstance() supportFragmentManager.beginTransaction()   .replace(R.id.root_fragment, feedFragment, TAG_FEED_FRAGMENT)   .commit()}

13. let 操作符

『let』操作符:如果對(duì)象的值不為空,則允許執(zhí)行這個(gè)方法。

//Javaif (currentUser != null) { text.setText(currentUser.name)}//instead Kotlinuser?.let { println(it.name)}

14. isNullOrEmpty | isNullOrBlank

我們需要在開發(fā) Android 應(yīng)用程序時(shí)多次驗(yàn)證。 如果你沒有使用 Kotlin 處理這個(gè)問題,你可能已經(jīng)在 Android 中發(fā)現(xiàn)了 TextUtils 類。

if (TextUtils.isEmpty(name)) { // alert the user!}public static boolean isEmpty(@Nullable CharSequence str) { return str == null || str.length() == 0;}

如果 name 都是空格,則 TextUtils.isEmpty 不滿足使用。則 isNullorBlank 可用。

public inline fun CharSequence?.isNullOrEmpty(): Boolean = this == null || this.length == 0public inline fun CharSequence?.isNullOrBlank(): Boolean = this == null || this.isBlank()// If we do not care about the possibility of only spaces...if (number.isNullOrEmpty()) { // alert the user to fill in their number!}// when we need to block the user from inputting only spacesif (name.isNullOrBlank()) { // alert the user to fill in their name!}

15. 避免 Kotlin 類的抽象方法

也是盡可能的使用 lambdas 。這樣可以實(shí)現(xiàn)更簡(jiǎn)潔直觀的代碼。例如在 Java 中的點(diǎn)擊監(jiān)聽為:

public interface OnClickListener { void onClick(View v);}

在 Java 中使用:

view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {  // do something }});

而在 Kotlin 中:

view.setOnClickListener { view -> // do something}//同時(shí)也可以為view.setOnClickListener { // do something}view.setOnClickListener() { // do something}

如果在 Kotlin 是使用單抽象方法的話:

view.setOnClickListener(object : OnClickListener { override fun onClick(v: View?) {  // do things }})

下面是另一種方法:

private var onClickListener: ((View) -> Unit)? = nullfun setOnClickListener(listener: (view: View) -> Unit) { onClickListener = listener}// later, to invokeonClickListener?.invoke(this)

16. with 函數(shù)

with 是一個(gè)非常有用的函數(shù),它包含在 Kotlin 的標(biāo)準(zhǔn)庫中。它接收一個(gè)對(duì)象和一個(gè)擴(kuò)展函數(shù)作為它的參數(shù),然后使這個(gè)對(duì)象擴(kuò)展這個(gè)函數(shù)。這表示所有我們?cè)诶ㄌ?hào)中編寫的代碼都是作為對(duì)象(第一個(gè)參數(shù)) 的一個(gè)擴(kuò)展函數(shù),我們可以就像作為 this 一樣使用所有它的 public 方法和屬性。當(dāng)我們針對(duì)同一個(gè)對(duì)象做很多操作的時(shí)候這個(gè)非常有利于簡(jiǎn)化代碼。

with(helloWorldTextView) { text = "Hello World!" visibility = View.VISIBLE}

17. Static Layout Import

Android 中最常用的代碼之一是使用 findViewById() 來獲取對(duì)應(yīng) View。

有一些解決方案,如 Butterknife 庫,可以節(jié)省很多代碼,但是 Kotlin 采取另一個(gè)步驟,允許您從一個(gè)導(dǎo)入的布局導(dǎo)入對(duì)視圖的所有引用。

例如,這個(gè) XML 布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView  android:id="@+id/tvHelloWorld"  android:layout_width="wrap_content"  android:layout_height="wrap_content"/></RelativeLayout>

在 Activity 中:

//導(dǎo)入對(duì)應(yīng)的 xmlimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {  super.onCreate(savedInstanceState)  setContentView(R.layout.activity_main)  //直接使用  tvHelloWorld.text = "Hello World!" }}

18. 用 Kotlin 實(shí)現(xiàn) POJO 類

在 Java 中

public class User { private String firstName; private String lastName; public String getFirstName() {  return firstName; } public void setFirstName(String firstName) {  this.firstName = firstName; } public String getLastName() {  return lastName; } public void setLastName(String lastName) {  this.lastName = lastName; }}

而在 Kotlin 中可以簡(jiǎn)化成:

class User { var firstName: String? = null var lastName: String? = null}

19. 減少 AsyncTash 的使用

搭配 Anko lib 使用。后臺(tái)和主線程的切換特別直觀和簡(jiǎn)單。uiThread 在主線程上運(yùn)行,并且我們不需要關(guān)心 Activity 的生命周期(pause 與 stop), 所以也不會(huì)出錯(cuò)了。

doAsync { var result = expensiveCalculation() uiThread {  toast(result) }}

20.apply 函數(shù)

它看起來于 with 很相似,但是是有點(diǎn)不同之處。apply 可以避免創(chuàng)建 builder 的方式來使用,因?yàn)閷?duì)象調(diào)用的函數(shù)可以根據(jù)自己的需要來初始化自己,然后 apply 函數(shù)會(huì)返回它同一個(gè)對(duì)象:

user = User().apply { firstName = Double lastName = Thunder}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)武林網(wǎng)的支持。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 上犹县| 德清县| 侯马市| 荔波县| 榆树市| 清徐县| 中方县| 云龙县| 肥城市| 留坝县| 辽源市| 江口县| 万全县| 革吉县| 济南市| 潞西市| 中牟县| 安多县| 潮安县| 南岸区| 淳安县| 景东| 永仁县| 铁岭县| 梁河县| 尉氏县| 眉山市| 桃园县| 永安市| 西昌市| 屯留县| 石城县| 崇州市| 博湖县| 永吉县| 雅安市| 磴口县| 道孚县| 印江| 固始县| 镇原县|