DataBindingが判れば、findViewById要らないじゃん
Kotlinでは、記述上、findViewByIdを使わないこともできるようになり、ソースはだいぶ見やすくなりました。でも、画面上の項目を意識しながらコーディングしなくちゃいけなくて、なんか違うな?泥臭いな。と思っていました。
DataBindingという技術を使えば、画面の項目をActivityから直接触るのではなく、Layout側に定義されたデータ定義をいじくることで、それが画面に反映することが出来るようになると。
Activity→Layoutの項目 と直接アクセスするのでは無く
Activity→Layoutのデータ定義→Layoutの項目 とすることで、Activityは項目の細かい挙動を考えなくてよくなるわけです。
細かい挙動は、Layout側で行う。Activityはそのもとになる情報を画面に渡すだけ。
修正前
以下は、ButtonとTextViewを画面に定義して、trueとfalseの表示を切り替えるだけのアプリ
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.TextView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var flag = true val buttona = findViewById<Button>(R.id.button) val texta = findViewById<TextView>(R.id.textview) buttona.setOnClickListener{ if (flag) { texta.setText("true") flag = false } else { texta.setText("false") flag = true } } } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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=".MainActivity"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="197dp" android:layout_marginEnd="8dp" android:layout_marginBottom="111dp" android:text="Button" app:layout_constraintBottom_toTopOf="@+id/textview" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
修正後
まず、build.gradle(アプリ)に追加して反映
1 2 3 4 5 6 7 |
android { ~~ dataBinding { enabled = true } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?xml version="1.0" encoding="utf-8"?> <!--以下追加 --> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="text" type="String" /> </data> <!--ここまで --> <androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{text}" ← データ定義の名前を設定 app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="197dp" android:layout_marginEnd="8dp" android:layout_marginBottom="111dp" android:text="Button" app:layout_constraintBottom_toTopOf="@+id/textview" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout> |
ここで、一回ビルドします。Android Studioがそれようのクラスを自動で作ってくれます。
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.databinding.DataBindingUtil import net.azside.kotlinTestYo.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var activityMainBinding: ActivityMainBinding // クラス名はXMLのファイル名+Bindingとなります override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) var flag = true activityMainBinding.button.setOnClickListener{ if (flag) { activityMainBinding.text = "true" flag = false } else { activityMainBinding.text = "false" flag = true } } } } |
ActivityMainBindingというクラス内に、Layout側のデータ項目、項目部品が定義されているので、そこをアクセスするようにする。もう画面をコントロールするというよりは、画面表示をうまくやってくれるクラスに指示を出すだけのイメージですね。