タイトル以上でも以下でもないので、早速本題に
BindingAdapterの定義
ViewBinding.kt
@BindingAdapter("app:touch_block")
fun setTouchBlock(view: View, isBlock: Boolean) {
view.setOnTouchListener { _, _ -> isBlock }
}
onTouch の戻り値をレイアウトから指定できるようにする
レイアウトでの指定方法
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:id="@+id/upper_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:touch_block="@{true}" // ← ココ
...
これで id/upper_layout の下に重なっているViewにタッチイベントが送られなくなりました。
細かい条件は指定できないので割り切りでタッチイベントを送りたくないという場合には使えそう。
app:touch_block="@{true}"
を app:touch_block="@{viewModel.touchBlock}"
とすればアプリの状態に応じてタッチイベントを 送る/送らない くらいの制御はできそうです。
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.hoge.SampleViewModel" />
</data>
...
<android.support.constraint.ConstraintLayout
android:id="@+id/upper_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:touch_block="@{viewModel.touchBlock}" // ← ココ
...
SampleViewModel.kt
SampleViewModel : ViewModel() {
val touchBlock = ObservableBoolean(true)
...
}