源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-09-05 05:58 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Kotlin开发的一些实用小技巧总结
[b]前言[/b] 随着Google I/O大会的召开,Google宣布将支持Kotlin作为Android的开发语言,最近关于Kotlin的文章、介绍就异常的活跃。 本文主要给大家介绍了关于Kotlin开发的一些实用小技巧,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 [b]1.Lazy Loading(懒加载)[/b] 延迟加载有几个好处。延迟加载能让程序启动时间更快,因为加载被推迟到访问变量时。 这在使用 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){ ... }
[b]2. 自定义 Getters/Setters[/b] 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)
}
[b]3. Lambdas[/b]
button.setOnClickListener { view ->
 startDetailActivity()
}

toolbar.setOnLongClickListener { 
 showContextMenu()
 true
}
[b]4.Data Classes(数据类)[/b] 数据类是一个简单版的 Class,它自动添加了包括 equals(), hashCode(), copy(), 和 toString() 方法。将数据与业务逻辑分开。
data class User(val name: String, val age: Int)
如果使用Gson解析Json的数据类,则可以使用默认值构造函数:
// Example with Gson's @SerializedName annotation
data class User(
 @SerializedName("name") val name: String = "",
 @SerializedName("age") val age: Int = 0
)
[b]5. 集合过滤[/b]
val users = api.getUsers()
// we only want to show the active users in one list
val activeUsersNames = items.filter { 
 it.active // the "it" variable is the parameter for single parameter lamdba functions
}
adapter.setUsers(activeUsers)
[b]6. Object Expressions(对象表达式)[/b] Object Expressions 允许定义单例。例如:
package com.savvyapps.example.util

import android.os.Handler
import android.os.Looper

// notice that this is object instead of class
object 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)
 }
});
这两个都基本上是相同的事情 - 创建一个类作为声明对象的单个实例。 [b]7. Companion Object(伴生对象)[/b] 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)
[b]8.Global Constants(全局常量)[/b] Kotlin 允许跨越整个应用的全局常量。通常,常量应尽可能减少其范围,但是全局都需要这个常量时,这是一个很好的方式。
const val PRESENTATION_MODE_PRESENTING = "presenting"
const val PRESENTATION_MODE_EDITING = "editing"
[b]9.Optional Parameters(可选参数)[/b] 可选参数使得方法调用更加灵活,而不必传递 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
[b]10. Extensions(扩展属性)[/b] 例如:在 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 [b]11. lateinit[/b] 对于 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: Toolbar
override 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")
}
[b]12. Safe Typecasting(安全转换) [/b] 在 Android 中需要安全类型转换。当您首先在 Kotlin 中进行类型转换时,您可以这样实现:
var feedFragment: FeedFragment? = supportFragmentManager
 .findFragmentByTag(TAG_FEED_FRAGMENT) as FeedFragment
但实际上这样只能导致崩溃。当调用『as』时,它将进行对象转换,但如果转换的对象为『null』时,则会报错。正确的使用方式应该是用『as?』:
var feedFragment: FeedFragment? = supportFragmentManager
 .findFragmentByTag(TAG_FEED_FRAGMENT) as? FeedFragment
if (feedFragment == null) {
 feedFragment = FeedFragment.newInstance()
 supportFragmentManager.beginTransaction()
   .replace(R.id.root_fragment, feedFragment, TAG_FEED_FRAGMENT)
   .commit()
}
[b]13. let 操作符[/b] 『let』操作符:如果对象的值不为空,则允许执行这个方法。
//Java
if (currentUser != null) {
 text.setText(currentUser.name)
}

//instead Kotlin
user?.let {
 println(it.name)
}
[b]14. isNullOrEmpty | isNullOrBlank [/b] 我们需要在开发 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 == 0

public 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 spaces
if (name.isNullOrBlank()) {
 // alert the user to fill in their name!
}
[b]15. 避免 Kotlin 类的抽象方法[/b] 也是尽可能的使用 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)? = null
fun setOnClickListener(listener: (view: View) -> Unit) {
 onClickListener = listener
}

// later, to invoke
onClickListener?.invoke(this)
[b]16. with 函数[/b] with 是一个非常有用的函数,它包含在 Kotlin 的标准库中。它接收一个对象和一个扩展函数作为它的参数,然后使这个对象扩展这个函数。这表示所有我们在括号中编写的代码都是作为对象(第一个参数) 的一个扩展函数,我们可以就像作为 this 一样使用所有它的 public 方法和属性。当我们针对同一个对象做很多操作的时候这个非常有利于简化代码。
with(helloWorldTextView) {
 text = "Hello World!"
 visibility = View.VISIBLE
}
[b]17. Static Layout Import[/b] 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 中:
//导入对应的 xml
import 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!"
 }
}
[b]18. 用 Kotlin 实现 POJO 类[/b] 在 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
}
[b]19. 减少 AsyncTash 的使用[/b] 搭配 [url=https://github.com/Kotlin/anko]Anko[/url] lib 使用。后台和主线程的切换特别直观和简单。uiThread 在主线程上运行,并且我们不需要关心 Activity 的生命周期(pause 与 stop), 所以也不会出错了。
doAsync {
 var result = expensiveCalculation()
 uiThread {
  toast(result)
 }
}
[b]20.apply 函数[/b] 它看起来于 with 很相似,但是是有点不同之处。apply 可以避免创建 builder 的方式来使用,因为对象调用的函数可以根据自己的需要来初始化自己,然后 apply 函数会返回它同一个对象:
user = User().apply {
 firstName = Double
 lastName = Thunder
}
[b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部