dev*_*vha 3 android android-support-library bottom-sheet
标准android BottomSheetBehavior具有树状态:隐藏,折叠和扩展。
我想允许用户在折叠和展开之间“保留”底页。现在,使用默认行为,它将根据最接近的位置捕捉到折叠或展开。我应该如何禁用此捕捉功能?
我将提出一个办法achievie这样的功能的View扩展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);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码应该做的只是将设置BottomSheet peekHeight为视图的高度。这里的关键是功能getBottomSheetBehaviour()。实现如下:
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;
}
Run Code Online (Sandbox Code Playgroud)
这只是检查其父项View是否设置了“ CoordinatorLayout.LayoutParams”。如果是,则设置适当的值BottomSheetBehavior.BottomSheetCallback(在下一部分中需要),更重要的是返回CoordinatorLayout.Behavior应该为的BottomSheetBehavior。
崩溃:
这里是一个[`BottomSheetBehavior.BottomSheetCallback.onSlide(查看bottomSheet,浮动slideOffset)``](https://developer.android.com/reference/android/support/design/widget/BottomSheetBehavior.BottomSheetCallback.html#onSlide (android 。 view.View,float))正是需要的。从[文档](https://developer.android.com/reference/android/support/design/widget/BottomSheetBehavior.BottomSheetCallback.html#onSlide ( android.view.View,float)):
随着此底板的向上移动,偏移量增加。从0到1,工作表介于折叠状态和展开状态之间;从-1到0,工作表介于隐藏状态和折叠状态之间。
这意味着,仅需要检查第二个参数即可进行崩溃检测:
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();
}
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2849 次 |
| 最近记录: |