androidandroid-touch-event

How does the Google Maps app let you drag the BottomSheet when it's behind the BottomNavigationView?


See this video so you know what I'm talking about. Notice how the BottomSheet can be dragged even when it's behind the BottomNavigationView:

https://streamable.com/balbx

I want to do the same thing but with a LinearLayout containing Buttons instead. I've tried numerous things and none work. I've subclassed LinearLayoutso that I can override onInterceptTouchEvent, and I'm using a GestureListener to detect a scroll.

I've posted a barebones test project on Github:

https://github.com/gavingt/BottomSheetTest

Or you can check out the relevant code below:

Here's MyLinearLayout:

public class MyLinearLayout extends LinearLayout {

GestureDetector gestureDetector;
boolean isScrolling = false;

public MyLinearLayout(Context context) {
    super(context);
    gestureDetector = new GestureDetector(context, new MyGestureListener());
}

public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    gestureDetector = new GestureDetector(context, new MyGestureListener());
}

public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    gestureDetector = new GestureDetector(context, new MyGestureListener());
}


@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    gestureDetector.onTouchEvent(ev);
    return isScrolling;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    isScrolling = gestureDetector.onTouchEvent(ev);
    return isScrolling;
}




class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                            float distanceX, float distanceY) {
        Log.i("TAG", "onScroll: ");

        return true;
    }

}

}

And here's activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#F00"
        android:overScrollMode="never"
        app:behavior_hideable="false"
        app:behavior_peekHeight="262dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/>

    <com.gavinsappcreations.bottomsheettest.MyLinearLayout
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/first_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:drawableLeft="@android:drawable/btn_radio"
            android:drawablePadding="2dp"
            android:maxLines="1"
            android:backgroundTint="#00F"
            android:paddingLeft="9dp"
            android:paddingRight="10dp"
            android:text="first button"
            android:textAllCaps="false"
            android:textSize="17dp"/>

        <Button
            android:id="@+id/second_button"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="50dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:backgroundTint="#00F"
            android:drawablePadding="2dp"
            android:drawableRight="@android:drawable/btn_radio"
            android:maxLines="1"
            android:paddingLeft="8dp"
            android:paddingRight="5dp"
            android:text="second button"
            android:textAllCaps="false"
            android:textSize="16dp"/>

    </com.gavinsappcreations.bottomsheettest.MyLinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Solution

  • I've solved this problem thanks to a Reddit user.

    The solution was to use his DelegatingLayout:

    https://gist.github.com/davidliu/c246a717f00494a6ad237a592a3cea4f

    See the Github project for the full implementation:

    https://github.com/gavingt/BottomSheetTest