0

I have a layout with some clickable views in it. I have implemented swipe gesture as per this. This is working great if you swipe in an empty area, but it does not work if you start swiping inside one of the views inside the layout.

How can this be avoided? Adding onSwipeListener to ever single view in the activity seems insane. Is there a better way?

under
  • 2,519
  • 1
  • 21
  • 40

2 Answers2

4

my working solution to this problem is using an invisible layer above the view with children as following:

  <RelativeLayout
        android:id="@+id/contentLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout"
        android:layout_alignStart="@+id/linearLayout"
        android:layout_alignParentTop="true"> 
        ... 
  </RelativeLayout>

  <View
        android:id="@+id/gesture_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout"
        android:layout_alignStart="@+id/linearLayout"
        android:layout_alignParentTop="true"
        android:focusable="true"
        android:focusableInTouchMode="true" />

where contentLayout contains multiple children including some buttons.

Second step is catching touch events on this gesture_view and proxying them to both contentLayout and my GestureDetector code (similar to the one you linked on the question) as following:

gesture_view.setOnTouchListener { _, event ->
    contentLayout.dispatchTouchEvent(event)
    gestureListener.onTouch(event)
    true
}

Note, you need to be careful about how you deliver the events to the view behind gesture_view. Simply contentLayout#onTouch will not work. It has to be contentLayout.dispatchTouchEvent

guness
  • 6,336
  • 7
  • 59
  • 88
  • 1
    the only one solution that works, adding the view (gesture_view) did the trick, thank you – i31nGo Jan 27 '21 at 09:42
1

Set android:clickable=false in your child views. This will prevent them from intercepting the touch events from the parent.

Dan Harms
  • 4,725
  • 2
  • 18
  • 28
  • Unfortunately, you don't have a lot of options. The buttons will need to handle the touch events to behave properly. – Dan Harms Jan 04 '19 at 22:09