bottomSheet屏蔽横向拖动
时间: 2023-05-28 11:05:25 浏览: 91
jQuery横向拖动产品展示代码
要在BottomSheet中禁用横向拖动,可以通过覆盖BottomSheetBehavior的onInterceptTouchEvent和onTouchEvent方法来实现。下面是一个示例:
```
public class NonDraggableBottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> {
public NonDraggableBottomSheetBehavior() {
super();
}
public NonDraggableBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
// 禁用横向拖动
if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
return true;
}
return super.onInterceptTouchEvent(parent, child, event);
}
@Override
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
// 禁用横向拖动
if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
return true;
}
return super.onTouchEvent(parent, child, event);
}
}
```
然后在布局中使用NonDraggableBottomSheetBehavior替换默认的BottomSheetBehavior即可:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content"/>
<FrameLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="200dp"
app:behavior_hideable="true"
app:behavior_peekHeight="50dp"
app:layout_behavior="your.package.NonDraggableBottomSheetBehavior">
<!-- BottomSheet内容 -->
</FrameLayout>
</LinearLayout>
```
阅读全文