LoginSignup
30
16

More than 5 years have passed since last update.

AndroidのKotlinでViewのレイアウト完了時処理を簡単に書く方法

Posted at

Androidアプリを開発していると、Viewのレイアウトが完了したタイミングで処理を書きたいことが、ままありますが、Kotlinであれば拡張関数を使って簡単に記述する方法があります。
こちらのサイトで紹介されています( https://antonioleiva.com/kotlin-ongloballayoutlistener/ )。

afterMeasured という拡張関数が、それです。

afterMeasured

inline fun <T : View> T.afterMeasured(crossinline f: T.() -> Unit) {
    viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            if (measuredWidth > 0 && measuredHeight > 0) {
                viewTreeObserver.removeOnGlobalLayoutListener(this)
                f()
            }
        }
    })
}

変な使用例ですが、ボタンを押下するたびTextViewの幅が広がっていくサンプルを作りました。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/buttonView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="CHANGE" />

    <TextView
        android:id="@+id/textView"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:background="#FF00FF00"
        />

</RelativeLayout>
MainActivity.kt

package jp.aboutsoft.aftermeasuredtest

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewTreeObserver
import kotlinx.android.synthetic.main.activity_main.*

inline fun <T : View> T.afterMeasured(crossinline f: T.() -> Unit) {
    viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            if (measuredWidth > 0 && measuredHeight > 0) {
                viewTreeObserver.removeOnGlobalLayoutListener(this)
                f()
            }
        }
    })
}

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        buttonView.setOnClickListener {

            textView.afterMeasured {
                // textViewのレイアウトが完了したら、幅を表示
                textView.text = "Width:${textView.width}"
            }

            // textViewの幅を50ずつ増やす
            val w = textView.width
            val lp = textView.layoutParams
            lp.width = w + 50
            textView.layoutParams = lp
        }
    }
}

Javaだと色々と面倒臭いことになる処理が、簡単に書けてしまいますね。

それでは、楽しいKotlin生活を!

30
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
16