请观看此视频,以便您知道我在说什么。请注意,即使BottomSheet
位于BottomNavigationView
后面,也可以拖动:
https://streamable.com/balbx
我想做同样的事情,但使用包含 Button
s 的LinearLayout
。我尝试了很多方法,但没有一种奏效。我已经LinearLayout
子类化,以便我可以覆盖onInterceptTouchEvent
,并且我正在使用GestureListener
来检测滚动。
我在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>
多亏了Reddit用户,我解决了这个问题。
解决方案是使用他的DemissionatingLayout:
https://gist.github.com/davidliu/c246a717f00494a6ad237a592a3cea4f
有关完整实现,请参阅 Github 项目:
https://github.com/gavingt/BottomSheetTest
你能试试这个吗:
- 在
LinearLayout
下方和google maps fragment
上方设置一个transparent RelativeLayout
。 touched
此transparent RelativeLayout
时,将焦点设置为google maps fragment
- 如果触摸了
LinearLayout
,则将focus
改回它。
希望这有帮助。