How does a Lambda expression work in Kotlin: setOnClickListener conversion (KAD 18), 180
By Antonio Leiva
Time: Mar 28,201 7
Link: https://antonioleiva.com/lambdas-kotlin-android/
Although I have mentioned a little about Lambda expressions in other articles, I would like to explain in depth how Lambda expressions work in Kotlin, and when we use the Java library, how they use a single Lambda expression to convert interfaces.
In particular, I want to give some examples to explain how to simplify the usage of the Android framework. We will also see the setOnClickListener In the Android view in detail.
SetOnClickListener Conversion
One of my favorite Kotlin features isWith some conventions, it is simplified.AndroidFramework work.
For example, the setOnClickListener function is defined in Java as follows:
1 public void setOnClickListener(OnClickListener l) {2 ...3 }
When we use it in Kotlin, we can do this:
1 fun setOnClickListener(l: (View) -> Unit)
This saves us the need to create an anonymous Implementation of the interface, greatly simplifying the initialization of the UI component.
Use
SetOnclickListenerOriginal Method
According to the above, we have saved enough code, that is, we must create the OnClickListener Anonymous class:
1 view.setOnClickListener(object : View.OnClickListener {2 override fun onClick(v: View?) {3 toast("Hello")4 }5 })
However, you will see the warning directly sent to you by the editor. We recommend that you use the Lambda Method.
The conversions you can perform:
1 view.setOnClickListener({ v -> toast("Hello") })
Simple, right? However, this can be further simplified.
If the last parameter of a function is a function, it can be moved out of parentheses.
Therefore, we can extract the listener as follows:
1 view.setOnClickListener() { v -> toast("Hello") }
If we have multiple parameters, the remaining parameters are in parentheses, even if these parameters are functions. Only the last parameter can be proposed.
If a function has only one parameter and is a function, parentheses can be deleted.
Instead of leaving empty parentheses, we 'd better delete the empty parentheses:
1 view.setOnClickListener { v -> toast("Hello") }
This constitutes a code block. In this way, we can define our own model language DSL.
A typical example is the Kotlin reference page. Their Component DSL creates HTML through code.
If you do not need Lambda parameters, you can delete the left part of the function.
If you only have one parameter, this is correct. You can delete a view without using view (v:
1 view.setOnClickListener { toast("Hello") }
In addition, when a function only receives parameters, it is not defined on the left, but reserved words are used, saving some characters.
For example, if we use a view to pass it to another method:
1 view.setOnClickListener { v -> doSomething(v) }
We can choose a simple it:
1 view.setOnClickListener { doSomething(it) }
Lambda expressions, your friends
The differences between these codes are very important.This not only saves you characters (about70%), Greatly improving the readability of the Code.
We only write the code that is really important, and skip the code that is not very useful.
If you want to learn more about this and be fluent in creating your own Android app, I suggest you get this free guide and learn how to build your first project, or purchase this book directly to learn how to create a complete application from the beginning.