0
0

More than 3 years have passed since last update.

バインディング式でクリックイベントを取得する

Last updated at Posted at 2021-03-18

DataBindingのバインディング式で、クリックイベントを取得する方法です。

android:onClick属性を設定する

クリックイベントを取得したいウィジェットに、android:onClickを指定します。

// 引数のViewなし
android:onClick="@{() -> viewModel.clickText()}"
// 引数のViewあり
android:onClick="@{viewModel::clickButton}"
// バインドされているデータを渡す
android:onClick="@{() -> viewModel.clickText(model)}"
ViewModel.kt
// android:onClick="@{() -> viewModel.clickText()}"の処理
fun clickText() {}

// android:onClick="@{viewModel::clickButton}"の処理
fun clickButton(view: View) {}

// android:onClick="@{() -> viewModel.clickText(model)}"の処理
fun clickText(model: Model) {}

その他のイベント

// EditTextの入力を監視する
android:onTextChanged="@{viewModel::onTextChange}"

// SwipeRefreshLayoutのリフレッシュイベントを取得する
app:onRefreshListener="@{() -> viewModel.refresh()}"
ViewModel.kt
// android:onTextChanged="@{viewModel::onTextChange}"の処理
fun onTextChange(s: CharSequence, start: Int, end: Int, count: Int) {}

// app:onRefreshListener="@{() -> viewModel.refresh()}"の処理
fun refresh() {}

まとめ

詳細は「developersの レイアウトとバインディング式 」に書いてあるので、見てみて下さい!

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