java - Kotlin 视图 setOnTouchListener 未触发(我参考了所有文档)
问题描述
两个文本视图属于视图。当我在视图上使用 setOnTouchListener 时,触摸只会在多次尝试中成功一次。
当我将 setOnTouchListener 用于作为 textview 一部分的 textAprName 时,触摸工作正常。
我想顺利地触摸两个文本视图。
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
var convertView:View? = convertView
val context = parent.context
val aprInfo = aprList[position]
var textAprName: TextView? = null
var textApr: TextView? = null
var holder: AprItemHolder? = null
if (convertView == null) {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = inflater.inflate(R.layout.apr_item_view, parent, false)
textAprName = convertView?.findViewById(R.id.text_apr_name) as TextView
textApr = convertView?.findViewById(R.id.text_apr) as TextView
holder = AprItemHolder()
holder.textAprName = textAprName
holder.textApr = textApr
convertView.tag = holder
} else {
holder = convertView.tag as AprItemHolder
textAprName = holder.textAprName
textApr = holder.textApr
}
textAprName?.text = aprInfo.name
textApr?.text = aprInfo.description
convertView?.setOnTouchListener @SuppressLint("ClickableViewAccessibility"){ v, event ->
var action: Int = MotionEventCompat.getActionMasked(event)
when (action) {
MotionEvent.ACTION_UP -> {
StandElin.instance!!.SendApr(aprInfo.name)
closeDialog()
}
}
true
}
return convertView
}
关于 layout.apr_item_view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="289dp"
android:layout_height="95dp"
android:descendantFocusability="blocksDescendants"
android:background="@drawable/btn_apr1">
<com.dkms.elin7.view.FontTextView
android:id="@+id/text_apr_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center"
android:text="APR 1"
android:textColor="@color/white"
android:textSize="30sp"
app:font="@string/font_gotham_book" />
<com.dkms.elin7.view.FontTextView
android:id="@+id/text_apr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="49dp"
android:gravity="center"
android:text="Stand,110,0"
android:textColor="@color/apr_text"
android:textSize="23sp"
app:font="@string/font_gotham_book" />
我将java项目转换为kotlin。
它适用于现有的 java,但不适用于 kotlin。
解决方案
还有一个像你这样的问题。
您也可以按照以下方式进行操作。
convertView.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
when (event?.action) {
MotionEvent.ACTION_DOWN -> //Do Something
}
return v?.onTouchEvent(event) ?: true
}
})
我认为onTouchListener
不像你写的那样工作。我找到了一个链接,您可以在其中更清楚地找到答案。
已编辑:您的源代码工作正常。我想我知道你在哪里得到“错误”。你实际上得到了“错误” convertView
。您想onTouchListener
在触摸视图时进行设置。但是,错误是您无法调用convertView
。我不知道为什么,您提供的源代码很少。这就是为什么我无法理解为什么?
现在,我可以给出替代答案。
我希望您添加id
到Relativelayout
您在此处实现的内容。然后,调用该布局。假设,id
该布局是view
. 然后,为该 kotlin 适配器实现以下源代码
view.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
when (event?.action) {
MotionEvent.ACTION_DOWN -> //Do Something
}
return v?.onTouchEvent(event) ?: true
}
})
我希望它会奏效。
推荐阅读
- r - 在 R 中的 if-else 语句中使用大小不等的数据帧
- zurb-foundation - 为什么在跟随原始演示时 Foundation Magellan 不起作用?
- python - 无法从 FTP 下载文件
- git - 如何在 Git 命令中指定多个路径?
- mongodb - $lookup 多个级别
- mongodb - mongoDB upsert 查询
- angular - Angular:使用路由对延迟加载模块进行单元测试
- node.js - 带有 OpenID 的 Vuejs/Node/Express 会话 ID 工作流
- python - 获取符合python的所有双引号或单引号包围的字符串
- python - 如何在 put_object 中指定标记?