Посмотрите это видео, чтобы вы знали, о чем я говорю. Обратите внимание, как BottomSheet
можно перетаскивать, даже если он находится за BottomNavigationView
:
https://streamable.com/balbx
Я хочу сделать то же самое, но с LinearLayout
, содержащим Button
с. Я перепробовал множество вещей, но ни одна из них не сработала. Я подкласс LinearLayout
, так что я могу переопределить onInterceptTouchEvent
, и я использую GestureListener
для обнаружения прокрутки.
Я опубликовал тестовый проект barebones на Github:
https://github.com/gavingt/BottomSheetTest
Или вы можете проверить соответствующий код ниже:
Вот 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;
}
}
}
А вот и 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>