Android 如何在Kotlin中通过数据绑定检测双击?
我试图通过使用数据绑定在Android 如何在Kotlin中通过数据绑定检测双击?,android,kotlin,data-binding,touch,listener,Android,Kotlin,Data Binding,Touch,Listener,我试图通过使用数据绑定在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"
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>
最后,这里是我在myViewModel
中的接收器方法:
val touchListener = View.OnTouchListener { v, detect ->
//Code to detect double-tap here??
return@OnTouchListener true
}
我已经尝试了几种不同的解决方案,但我找不到一种可以用上述方法实现的解决方案。有一个小技巧,可以通过测量第一次点击和第二次点击之间的时间来检测双击 在ViewModel类中:
// 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
}
//这是第一次和第二次轻敲之间的最大持续时间
//这是一个相对值,你可以操纵它。
伴星{
私有常量值双击增量:长=300//毫秒
}
//用于跟踪抽头定时的变量:
var lastClickTime:Long=0
@SuppressLint(“ClickableViewAccessibility”)
val touchListener=View.OnTouchListener{v,detect->
如果(detect.action==MotionEvent.action\u DOWN){//避免向上事件
//检测双抽头:
val clickTime=System.currentTimeMillis()
如果(单击时间-上次单击时间<双击增量){
Toast.makeText(应用程序,“双击”,Toast.LENGTH\u SHORT.show())
}
lastClickTime=单击时间
}
return@OnTouchListener真的
}
通过测量第一次轻击和第二次轻击之间的时间来检测双击有一个小技巧
在ViewModel类中:
// 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
}
//这是第一次和第二次轻敲之间的最大持续时间
//这是一个相对值,你可以操纵它。
伴星{
私有常量值双击增量:长=300//毫秒
}
//用于跟踪抽头定时的变量:
var lastClickTime:Long=0
@SuppressLint(“ClickableViewAccessibility”)
val touchListener=View.OnTouchListener{v,detect->
如果(detect.action==MotionEvent.action\u DOWN){//避免向上事件
//检测双抽头:
val clickTime=System.currentTimeMillis()
如果(单击时间-上次单击时间<双击增量){
Toast.makeText(应用程序,“双击”,Toast.LENGTH\u SHORT.show())
}
lastClickTime=单击时间
}
return@OnTouchListener真的
}
非常好的解决方案,谢谢!非常好的解决方案,谢谢!