ソース
class MainActivity : Activity() {
private var views = mutableSetOf<View>()
private var debugCount = 0
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// タップ時に判定したいViewを詰める
views.add(findViewById(R.id.redView))
views.add(findViewById(R.id.purpleView))
views.add(findViewById(R.id.greenView))
views.add(findViewById(R.id.yellowView))
findViewById<ScrollView>(R.id.scrollView).setOnTouchListener(View.OnTouchListener(
fun (view, event): Boolean {
if (event.action == MotionEvent.ACTION_DOWN) {
val x = event.rawX.toInt()
val y = event.rawY.toInt()
for (view in views) {
if (getRect(view).contains(x, y)) {
Log.d("", "${debugCount++}: ${resources.getResourceEntryName(view.id)}がタップされた。")
break
}
}
}
return false
}
))
}
private fun getRect(view: View): Rect {
val rect = Rect()
view.getGlobalVisibleRect(rect)
return rect
}
}
レイアウト
<?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">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="100dp">
<View
android:id="@+id/redView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="72dp"
android:layout_marginLeft="72dp"
android:layout_marginTop="152dp"
android:background="#F44336"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="211dp"
android:layout_height="179dp"
android:layout_marginTop="312dp"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
android:background="#FFC107"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:id="@+id/purpleView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="56dp"
android:layout_marginRight="56dp"
android:background="#9C27B0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:id="@+id/greenView"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginTop="556dp"
android:background="#4CAF50"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/yellowView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="820dp"
android:background="#FFEB3B"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
実際の動作
以上
追記
これ四角形しか判定できないね。素直にタップのイベントを付けてあげましょう。