首页 > 开发 > 其他 > 正文

Kotlin开发的一些实用小技巧总结

2019-10-20 15:57:27
字体:
来源:转载
供稿:网友

前言

随着Google I/O大会的召开,Google宣布将支持Kotlin作为Android的开发语言,最近关于Kotlin的文章、介绍就异常的活跃。

本文主要给大家介绍了关于Kotlin开发的一些实用小技巧,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

1.Lazy Loading(懒加载)

延迟加载有几个好处。延迟加载能让程序启动时间更快,因为加载被推迟到访问变量时。 这在使用 Kotlin 的 Android 应用程序而不是服务器应用程序中特别有用。对于 Android 应用,我们自然希望减少应用启动时间,以便用户更快地看到应用内容,而不是等待初始加载屏幕。

懒加载也是更有效率的内存,因为我们只需要调用资源才能将资源加载到内存中。例如:

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

如果用户从没有调用 GankApi ,则永远不会加载。因此也不会占用所需资源。

当然懒加载也能较好的用于封装初始化:

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

如果你不担心多线程问题或者想提高更多的性能,你也可以使用

lazy(LazyThreadSafeMode.NONE){ ... }

2. 自定义 Getters/Setters

Kotlin 会自动的使用 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(数据类)

数据类是一个简单版的 Class,它自动添加了包括 equals(), hashCode(), copy(), 和 toString() 方法。将数据与业务逻辑分开。

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

如果使用Gson解析Json的数据类,则可以使用默认值构造函数:

// 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(对象表达式)

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 则可以直接调用静态类方法:

ThreadUtil.onMainThread(runnable)

以类似的方式,我们创建对象而不是匿名内部类:

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) }});

这两个都基本上是相同的事情 - 创建一个类作为声明对象的单个实例。

7. Companion Object(伴生对象)

Kotlin 是没有静态变量与方法的。相对应的,可以使用伴生对象。伴生对象允许定义的常量和方法,类似于 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 允许跨越整个应用的全局常量。通常,常量应尽可能减少其范围,但是全局都需要这个常量时,这是一个很好的方式。

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

9.Optional Parameters(可选参数)

可选参数使得方法调用更加灵活,而不必传递 null 或默认值。 例如:这在定义动画时:

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(扩展属性)

例如:在 Activity 调用键盘的隐藏

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}

推荐一个收集 Extensions 的网站 。 kotlinextensions.com

11. lateinit

对于 Null 的检查是 Kotlin 的特点之一,所以在数据定义时,初始化数据。但有一些在 Android 中某些属性需要在 onCreate() 方法中初始化。

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

如果是基础数据类型:

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(安全转换)

在 Android 中需要安全类型转换。当您首先在 Kotlin 中进行类型转换时,您可以这样实现:

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

但实际上这样只能导致崩溃。当调用『as』时,它将进行对象转换,但如果转换的对象为『null』时,则会报错。正确的使用方式应该是用『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』操作符:如果对象的值不为空,则允许执行这个方法。

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

14. isNullOrEmpty | isNullOrBlank

我们需要在开发 Android 应用程序时多次验证。 如果你没有使用 Kotlin 处理这个问题,你可能已经在 Android 中发现了 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 。这样可以实现更简洁直观的代码。例如在 Java 中的点击监听为:

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}//同时也可以为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 函数

with 是一个非常有用的函数,它包含在 Kotlin 的标准库中。它接收一个对象和一个扩展函数作为它的参数,然后使这个对象扩展这个函数。这表示所有我们在括号中编写的代码都是作为对象(第一个参数) 的一个扩展函数,我们可以就像作为 this 一样使用所有它的 public 方法和属性。当我们针对同一个对象做很多操作的时候这个非常有利于简化代码。

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

17. Static Layout Import

Android 中最常用的代码之一是使用 findViewById() 来获取对应 View。

有一些解决方案,如 Butterknife 库,可以节省很多代码,但是 Kotlin 采取另一个步骤,允许您从一个导入的布局导入对视图的所有引用。

例如,这个 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 中:

//导入对应的 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 实现 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 中可以简化成:

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

19. 减少 AsyncTash 的使用

搭配 Anko lib 使用。后台和主线程的切换特别直观和简单。uiThread 在主线程上运行,并且我们不需要关心 Activity 的生命周期(pause 与 stop), 所以也不会出错了。

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

20.apply 函数

它看起来于 with 很相似,但是是有点不同之处。apply 可以避免创建 builder 的方式来使用,因为对象调用的函数可以根据自己的需要来初始化自己,然后 apply 函数会返回它同一个对象:

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对CuoXin错新网的支持。


注:相关教程知识阅读请移步到kotlin教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表