Fun Facts About OnMeasure() and OnLayout()

Help you understander better about measure and layout

Guowei Lv

2 minute read

Let’s understand more about Android view’s measure and layout process, and have some fun along the way. Let’s say we have this: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&#34; xmlns:app="http://schemas.android.com/apk/res-auto&#34; xmlns:tools="http://schemas.android.com/tools&#34; android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.example.stubornview.StubbornView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/design_default_color_primary" /> </LinearLayout> class StubbornView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { /** * 1. Parent LinearLayout combines developer's requirements * and the available space of itself into MeasureSpecs and * pass them here.

Guowei Lv

3 minute read

Yes, there is documentation and tons of sample apps out there. But they are all in a top down approach, meaning they give you the final result of how to use it first, and never tells you why and what is behind the scenes. I will fix that in this article, and present a bottom up approach which leads to much better and deeper understanding. First, at the bottom of things, we need to define what is ViewModel.

Guowei Lv

2 minute read

It is quite common in Android that we need some expandable widget to show and hide information. This is my first attempt, note that this is only a “sketch”, and I intentionally leave some room for improvement. One interesting detail worth mentioning is how the animation is done. I used the reverse() function to play animation backwards in order to achieve a smooth and continuous feel.

Here is the code:

How by Lazy Works

Implement a simpler version of by lazy

Guowei

2 minute read

by lazy is implemented by using the “property delegation” in Kotlin. But if you look into the source code and trying to understand what is going on, it can be confusing, because it is full of locks and generics and where’s the getValue()` function they say that the delegation must implement?? In the handmade spirit (best way to learn is by doing it yourself), let’s do a stripped down version ourselves.

Kotlin Noinline and Crossinline

What is noinline and crossinline?

Guowei Lv

4 minute read

Compile time constant const val NAME = "Guowei" fun main() { tv.text = NAME } After compile it will (almost) look like this: // Imaginary code fun main() { tv.text = "Guowei" } inline function We can do similar things to functions, by adding keyword inline. inline fun hello() { println("hello") } fun main() { hello() } So at compile time, the hello() function will be copied to the calling place: