我已经使用 NestedScrollView 实现了底页行为。并且想知道在外面触摸时是否可以隐藏底部视图。
10 回答
最后我能够做到这一点,
使用了以下代码行:
@Override public boolean dispatchTouchEvent(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (mBottomSheetBehavior.getState()==BottomSheetBehavior.STATE_EXPANDED) {
Rect outRect = new Rect();
bottomSheet.getGlobalVisibleRect(outRect);
if(!outRect.contains((int)event.getRawX(), (int)event.getRawY()))
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
return super.dispatchTouchEvent(event);
}
希望它能拯救某人的一整天!
感谢OP的问题/答案。我使用了他的代码,但改进了它的清洁度并想分享。您可以直接在 BottomSheetBehavior 中编写代码,而不是扩展 View 并添加接口。像这样:
AutoCloseBottomSheetBehavior.java
import android.content.Context;
import android.graphics.Rect;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class AutoCloseBottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> {
public AutoCloseBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN &&
getState() == BottomSheetBehavior.STATE_EXPANDED) {
Rect outRect = new Rect();
child.getGlobalVisibleRect(outRect);
if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
return super.onInterceptTouchEvent(parent, child, event);
}
}
然后您只需将其添加到您的 XML 布局中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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">
... your normal content here ...
<SomeLayout... />
... the bottom sheet with the behavior
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_behavior="<com.package.name.of.the.class>.AutoCloseBottomSheetBehavior">
... the bottom sheet views
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
对于活动:
@Override
public boolean dispatchTouchEvent(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (mBottomSheetBehavior.getState()==BottomSheetBehavior.STATE_EXPANDED) {
Rect outRect = new Rect();
bottomSheet.getGlobalVisibleRect(outRect);
if(!outRect.contains((int)event.getRawX(), (int)event.getRawY()))
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
return super.dispatchTouchEvent(event);
}
对于片段:在Activity中使用相同的方法,例如,
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (fragment != null && fragment instanceof HomeFragment) {
((HomeFragment) fragment).hideBottomSheetFromOutSide(event);
}
}
return super.dispatchTouchEvent(event);
}
并在片段中创建方法,例如:
/**
* Calling from Dashboard Activity
*
* @param event Motion Event
*/
public void hideBottomSheetFromOutSide(MotionEvent event) {
if (isBottomSheetMenuExpanded()) {
Rect outRect = new Rect();
mBinding.homeBottomSheetLayout.getGlobalVisibleRect(outRect);
if (!outRect.contains((int) event.getRawX(), (int) event.getRawY()))
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
希望它会帮助你。
谢谢你。
为您的主布局设置点击监听器(在这种情况下为坐标布局)
@OnClick(R.id.coordinateLayout)
public void onClickView(View view) {
if (sheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
注意:Butterknife 用于单击,否则在活动的 onCreate 中使用下面的代码。
CoordinateLayout layout = (CoordinateLayout) findViewById(R.id. coordinateLayout);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
});
您可以调用以下代码在单击外部时关闭底部工作表对话框。
BottomSheetDialog dialog = new BottomSheetDialog(context);
dialog.setContentView(R.layout.bottom_sheet);
dialog.setCanceledOnTouchOutside(true);
dialog.show();
someViewToClickOn.setOnClickListener(v ->
behavior.setState(BottomSheetBehavior.STATE_HIDDEN));
这也有效!我第一次使用BottomSheetBehavior.STATE_COLLAPSED
哪个不起作用
对我来说这是一个简单的setCancelable(true);
即
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.layout_additional_prices, null);
unbinder = ButterKnife.bind(this, contentView);
dialog.setContentView(contentView);
dialog.setOnKeyListener(new BottomSheetBackDismissListener());
//makeBottomSheetFullScreen(getActivity(), mBottomSheetBehaviorCallback, contentView);
setCancelable(true);
}
我发现使用 OP 的答案或 AutoCloseBottomSheetBehavior.java 版本(来自 Budius)的 UX 问题。我更改了 AutoCloseBottomSheetBehavior 代码,这种方式对我来说似乎更清晰,并且不会导致任何 UX 问题(代码在 Kotlin 中):
class AutoCloseBottomSheetBehavior<V : View>(
context: Context,
attrs: AttributeSet
) : BottomSheetBehavior<V>(context, attrs) {
private val gestureDetector: GestureDetectorCompat =
GestureDetectorCompat(context, object : GestureDetector.SimpleOnGestureListener() {
override fun onDown(event: MotionEvent?): Boolean {
return true
}
override fun onSingleTapUp(e: MotionEvent?): Boolean =
when {
state == STATE_EXPANDED -> {
state = STATE_COLLAPSED
true
}
state == STATE_COLLAPSED && isHideable -> {
state = STATE_HIDDEN
true
}
else -> false
}
})
@SuppressLint("ClickableViewAccessibility")
override fun onLayoutChild(parent: CoordinatorLayout, child: V, layoutDirection: Int): Boolean {
parent.setOnTouchListener { _, event ->
gestureDetector.onTouchEvent(event)
}
return super.onLayoutChild(parent, child, layoutDirection)
}
}
每当用户在父 CoordinatorLayout 上执行单击时,这会折叠/隐藏底部工作表,并且还处理底部工作表可隐藏的情况(如果它处于 COLLAPSED 状态,我们希望隐藏它)。
有两种方法可以使对话框可取消:
设置是否可以使用 BACK 键取消此对话框。
爪哇
dialog.setCancelable(true);
科特林
dialog.setCancelable(true)
设置在窗口边界外触摸时是否取消此对话框。
爪哇
dialog.setCanceledOnTouchOutside(true);
科特林
dialog.setCanceledOnTouchOutside(true)
有很多人找到了在片段上实现 dispatchTouchEvent 的方法。所以他们可以这样做:
按照定义创建自定义布局:
public class DispatchTouchEvent extends LinearLayout {
public interface onDispatchEvent
{
void dispatchEvent(MotionEvent e);
}
private onDispatchEvent dispatchEvent;
public DispatchTouchEvent(Context context) {
super(context);
}
public DispatchTouchEvent(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DispatchTouchEvent(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setDispatchEvent(onDispatchEvent dispatchEvent)
{
this.dispatchEvent=dispatchEvent;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(dispatchEvent!=null)
{
dispatchEvent.dispatchEvent(ev);
}
return super.dispatchTouchEvent(ev);
}
}
现在将此布局用作片段布局的基础。内部片段将此布局初始化为:
public class ABC extends fragment implements DispatchTouchEvent.onDispatchEvent
{
DispatchTouchEvent dispatchTouchEvent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
....
dispatchTouchEvent = (DispatchTouchEvent)rootView.findViewById(R.id.dispatch_event);
dispatchTouchEvent.setDispatchEvent(this);
....
}
@Override
public void dispatchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (mBottomSheetBehavior.getState()==BottomSheetBehavior.STATE_EXPANDED)
{
Rect outRect = new Rect();
bottomSheet.getGlobalVisibleRect(outRect);
if(!outRect.contains((int)event.getRawX(), (int)event.getRawY()))
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
}
}