【Android】DataBinding

DataBindingが判れば、findViewById要らないじゃん

Kotlinでは、記述上、findViewByIdを使わないこともできるようになり、ソースはだいぶ見やすくなりました。でも、画面上の項目を意識しながらコーディングしなくちゃいけなくて、なんか違うな?泥臭いな。と思っていました。

DataBindingという技術を使えば、画面の項目をActivityから直接触るのではなく、Layout側に定義されたデータ定義をいじくることで、それが画面に反映することが出来るようになると。

Activity→Layoutの項目 と直接アクセスするのでは無く
Activity→Layoutのデータ定義→Layoutの項目 とすることで、Activityは項目の細かい挙動を考えなくてよくなるわけです。

細かい挙動は、Layout側で行う。Activityはそのもとになる情報を画面に渡すだけ。

修正前

以下は、ButtonとTextViewを画面に定義して、trueとfalseの表示を切り替えるだけのアプリ

MainActivity.kt


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

<?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(アプリ)に追加して反映

android {
     ~~
     dataBinding {
        enabled = true
    }

}

activity_main.xml

<?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

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側のデータ項目、項目部品が定義されているので、そこをアクセスするようにする。もう画面をコントロールするというよりは、画面表示をうまくやってくれるクラスに指示を出すだけのイメージですね。

スポンサーリンク
レクタングル大

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

スポンサーリンク
レクタングル大
%d人のブロガーが「いいね」をつけました。