<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/actions"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/action_previous"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/action_next"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_weight="3"/>
<View
android:id="@+id/action_next"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/action_previous"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_weight="7"/>
</androidx.constraintlayout.widget.ConstraintLayout>
ImageActivity.kt
swipe_parent.setOnTouchListener { v, event ->
Toast.makeText(this@ImageActivity, "OnTouch called $v, $event", Toast.LENGTH_SHORT).show()
return@setOnTouchListener true
}
action_previous.setOnClickListener {
Toast.makeText(this, "Previous", Toast.LENGTH_SHORT).show()
}
action_next.setOnClickListener {
Toast.makeText(this, "Next", Toast.LENGTH_SHORT).show()
}
我在许多评论中看到,如果子视图有一个onClickListener,那么OnTouchListener的调用就不会被传递给父视图(如果视图的结构如上)。而且从逻辑上来说,这也是合理的,因为很难区分点击和触摸。
如果这是我的要求,我如何实现?
最快解决这个问题的方法是用ontouch代替onclick,但是模仿onclick的功能,在子视图ontouch中检查这个条件,它只对action_down的情况下执行,这对你的情况会有帮助。
if (event.action == MotionEvent.ACTION_DOWN)
我也遇到了同样的问题。我是通过设置
android:clickable="false"
android:descendantFocusability="blocksDescendants"
父视图中。