I have a layout with 5 items in a LinearLayout in a ScrollView. I want the items to be clickable and not interfere with scrolling.
The problem is scrolling. If drag up/down on an item, it doesn't scroll. If I drag up/down on the margins between the items, it does scroll.
How can I handle click events for items AND allow scrolling when the user drags up/down on an item?
Note: this isn't about efficient layouts or just using a RecyclerView (which seems like overkill). I want to know how to scroll and handle children being clicked.
I've tried various combinations of click and touch listeners, GestureDetector, onInterceptTouchEvent... but I only seem to be able to either click or scroll, not both.
I followed these tutorials/answers and none have worked:
- Android tutorial on intercepting touch events
- How to vary between child and parent view group touch events
- OnClickListener on scrollView
There must be something simple I'm missing... some flag or something?
Layout:
<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" /> ... Activity
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 -> ... ... } }