【发布时间】:2021-01-09 22:55:53
【问题描述】:
我在 ScrollView 的 LinearLayout 中有一个包含 5 个项目的布局。我希望这些项目是可点击的,并且不会干扰滚动。
问题在于滚动。如果向上/向下拖动一个项目,它不会滚动。如果我在项目之间的边距上上下拖动,它会滚动。
如何处理项目的点击事件并在用户向上/向下拖动项目时允许滚动?
注意:这与高效布局或仅使用 RecyclerView 无关(这看起来有点过头了)。我想知道如何滚动和处理被点击的孩子。
我尝试了点击和触摸侦听器、GestureDetector、onInterceptTouchEvent 的各种组合...但我似乎只能点击或滚动,而不是两者。
我遵循了这些教程/答案,但没有一个有用:
- Android tutorial on intercepting touch events
- How to vary between child and parent view group touch events
- OnClickListener on scrollView
一定有一些简单的东西我错过了......一些标志或什么?
布局:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/itemOne"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:margin="16dp" />
<LinearLayout
android:id="@+id/itemTwo"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:margin="16dp" />
...
活动
override fun onCreate(savedInstanceState: Bundle?) {
itemOne.setOnClickListener(this)
itemTwo.setOnClickListener(this)
...
}
override fun onClick(view: View?) {
when (view?.id) {
R.id.itemOne-> ...
R.id.itemTwo -> ...
...
}
}
【问题讨论】:
标签: android click touch scrollview