Android BottomSheetBehavior,如何禁用快照? [英] Android BottomSheetBehavior, how to disable snap?

查看:75
本文介绍了Android BottomSheetBehavior,如何禁用快照?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准android BottomSheetBehavior具有树状态:隐藏,折叠和展开.

Standard android BottomSheetBehavior has tree state: hidden, collapsed and expanded.

我想允许用户在折叠和展开之间离开"底页.现在,使用默认行为,它将根据最接近的位置捕捉​​到折叠或展开.我应该如何禁用此快照功能?

I want to allow user to "leave" bottom sheet between collapsed and expanded. Now, with the default behavior, it will snap to collapsed or expanded based which is closest. How should I disable this snap functionality?

推荐答案

我将为实现View扩展BottomSheetDialogFragment的功能提供一种方法.

I will present a way to achievie such functionality for a View extending BottomSheetDialogFragment.

扩展:

首先是onResume:

@Override
public void onResume() {
    super.onResume();
    addGlobaLayoutListener(getView());
}

private void addGlobaLayoutListener(final View view) {
    view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            setPeekHeight(v.getMeasuredHeight());
            v.removeOnLayoutChangeListener(this);
        }
    });
}

public void setPeekHeight(int peekHeight) {
    BottomSheetBehavior behavior = getBottomSheetBehaviour();
    if (behavior == null) {
        return;
    }
    behavior.setPeekHeight(peekHeight);
}

上面的代码应该做的只是将BottomSheet peekHeight设置为视图的高度.这里的关键是功能getBottomSheetBehaviour().实现如下:

What the code above is supposed to do is just setting the BottomSheet peekHeight to the heigth of the view. The key here is the function getBottomSheetBehaviour(). The implementation is below:

private BottomSheetBehavior getBottomSheetBehaviour() {
    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) ((View) getView().getParent()).getLayoutParams();
    CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
    if (behavior != null && behavior instanceof BottomSheetBehavior) {
        ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
        return (BottomSheetBehavior) behavior;
    }
    return null;
}

这只是检查View的父级是否设置了'CoordinatorLayout.LayoutParams'.如果是,请设置适当的 BottomSheetBehavior.BottomSheetCallback ( (在下一部分中需要),更重要的是返回CoordinatorLayout.Behavior,应该是BottomSheetBehavior.

This just check if the parent of View has 'CoordinatorLayout.LayoutParams' set. If yes, sets appropriate BottomSheetBehavior.BottomSheetCallback (which is needed in the next part), and more importantly returns the CoordinatorLayout.Behavior, which is supposed to be BottomSheetBehavior.

合拢:

这里有一个[`BottomSheetBehavior.BottomSheetCallback.onSlide(查看bottomSheet,float slideOffset)'']] <

Here a [`BottomSheetBehavior.BottomSheetCallback.onSlide (View bottomSheet, float slideOffset)``](https://developer.android.com/reference/android/support/design/widget/BottomSheetBehavior.BottomSheetCallback.html#onSlide(android.view.View, float)) is just exactly what is needed. From the [documentation](https://developer.android.com/reference/android/support/design/widget/BottomSheetBehavior.BottomSheetCallback.html#onSlide(android.view.View, float)):

此底纸向上移动时,偏移量增加.从0到1,工作表介于折叠状态和展开状态之间;从-1到0,工作表介于隐藏状态和折叠状态之间.

Offset increases as this bottom sheet is moving upward. From 0 to 1 the sheet is between collapsed and expanded states and from -1 to 0 it is between hidden and collapsed states.

这意味着,仅需要检查第二个参数即可进行崩溃检测:

This means, that just checking the second parameter is needed for collapse detection:

在同一类中定义BottomSheetBehavior.BottomSheetCallback:

private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
            dismiss();
        }
    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        if (slideOffset < 0) {
            dismiss();
        }
    }
};

这篇关于Android BottomSheetBehavior,如何禁用快照?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆