如何在 Kotlin 中使用数据绑定检测双击?
How to detect double-tap with databinding in Kotlin?
我正在尝试使用数据绑定检测 ImageView
上的双击。
我已经这样设置了我的布局XML
:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="myViewModel"
type="com.virtualsheetmusic.vsheetmusic.viewer.myViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/viewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/drawingCanvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/myrect"
android:visibility="visible"
app:onTouch="@{musicViewerViewModel.touchListener}"
android:contentDescription="Canvas for anotations, gestures, etc" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
这是我的绑定适配器:
@BindingAdapter("app:onTouch")
fun setXMLTouchListener(layout : ImageView , listener : View.OnTouchListener)
{
layout.setOnTouchListener(listener)
}
最后,这是我在 myViewModel
中的接收方法:
val touchListener = View.OnTouchListener { v, detect ->
//Code to detect double-tap here??
return@OnTouchListener true
}
我已经尝试了几种不同的解决方案,但我找不到可以在上面的方法中实现的解决方案。
通过测量第一次点击和第二次点击之间的时间来检测双击。
在 ViewModel 中 class:
// This is max duration between first and second taps
// which is a relative value, You can manipulate it.
companion object {
private const val DOUBLE_CLICK_DELTA: Long = 300 // Millisec
}
// a variable for tracking the tap timing:
var lastClickTime: Long = 0
@SuppressLint("ClickableViewAccessibility")
val touchListener = View.OnTouchListener { v, detect ->
if (detect.action == MotionEvent.ACTION_DOWN) { // Avoid UP event
// Detecting double-tap:
val clickTime = System.currentTimeMillis()
if (clickTime - lastClickTime < DOUBLE_CLICK_DELTA) {
Toast.makeText(application, "Double tap", Toast.LENGTH_SHORT).show()
}
lastClickTime = clickTime
}
return@OnTouchListener true
}
我正在尝试使用数据绑定检测 ImageView
上的双击。
我已经这样设置了我的布局XML
:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="myViewModel"
type="com.virtualsheetmusic.vsheetmusic.viewer.myViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/viewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/drawingCanvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/myrect"
android:visibility="visible"
app:onTouch="@{musicViewerViewModel.touchListener}"
android:contentDescription="Canvas for anotations, gestures, etc" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
这是我的绑定适配器:
@BindingAdapter("app:onTouch")
fun setXMLTouchListener(layout : ImageView , listener : View.OnTouchListener)
{
layout.setOnTouchListener(listener)
}
最后,这是我在 myViewModel
中的接收方法:
val touchListener = View.OnTouchListener { v, detect ->
//Code to detect double-tap here??
return@OnTouchListener true
}
我已经尝试了几种不同的解决方案,但我找不到可以在上面的方法中实现的解决方案。
通过测量第一次点击和第二次点击之间的时间来检测双击。
在 ViewModel 中 class:
// This is max duration between first and second taps
// which is a relative value, You can manipulate it.
companion object {
private const val DOUBLE_CLICK_DELTA: Long = 300 // Millisec
}
// a variable for tracking the tap timing:
var lastClickTime: Long = 0
@SuppressLint("ClickableViewAccessibility")
val touchListener = View.OnTouchListener { v, detect ->
if (detect.action == MotionEvent.ACTION_DOWN) { // Avoid UP event
// Detecting double-tap:
val clickTime = System.currentTimeMillis()
if (clickTime - lastClickTime < DOUBLE_CLICK_DELTA) {
Toast.makeText(application, "Double tap", Toast.LENGTH_SHORT).show()
}
lastClickTime = clickTime
}
return@OnTouchListener true
}