Kotlin Android Extensions
「Kotlin Android Extensions」というAndroid用の拡張機能について共有します。
Viewの操作がすごく楽になります。
詳しくは下記をご確認ください。
https://kotlinlang.org/docs/tutorials/android-plugin.html
楽になる
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="com.example.hoge.hoge.activity.MainActivity">
<TextView
android:id="@+id/feed"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>
</android.support.constraint.ConstraintLayout>
例えば上記のようなViewにTextをコードから挿入するとき今まで下記のように書いていたと思います。
TextView text = (TextView)findViewById(R.id.feed);
text.setext("hogehoge");
でも「Kotlin Android Extensions」を使うと
feed.text = "hogehoge"
素晴らしすぎますね。
他にもボタンイベントは
button.setOnClickListener{
}
のようにかけたり
ListViewにアダプターを設定するときは
val adapter = FeedAdapter(this, 0, createExampleData())
feed.adapter = adapter
すばらしい。