【发布时间】:2021-07-25 07:03:44
【问题描述】:
我有以下 kotlin 代码,但无法调用 onTouch\click 侦听器。 我正在使用模拟器,我在很多地方多次单击该片段,但似乎没有任何东西让听众打电话。我设置了断点并打印\日志,却一无所获...
gridview 有一个 onItemClick。如果我删除了gridview - 触摸监听器工作,但我确实需要gridview 和onItemClick。那么如何将两者结合起来呢?
片段 xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
tools:context=".fragments.MainAppFragment">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="150dp"
android:id="@+id/my_grid_view"
android:horizontalSpacing="15dp"
android:numColumns="auto_fit"
android:verticalSpacing="15dp"
/>
</FrameLayout>
片段类:
class MainAppFragment : Fragment(){
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_main_app, container, false)
view.setOnClickListener {
Log.d("TAG", "onTouch entered");
}
view.setOnTouchListener { v, event ->
print(event.action)
true
}
return view
}
}
【问题讨论】:
标签: android kotlin touch-event ontouchlistener