一、BottomSheetDialog简介
用途:底部弹起的view或dialog。
实现:其关键也是CoordinatorLayout与Behavior
要求: 采用View的形式展示的话,用于展示的View必须具备如下两个要求:
1,View类必须支持嵌套滚动。
2,View类必须是CoordinatorLayout的直接子类。
二、简单使用
1、Dialog的形式
if (mBottomSheetDialog == null){
mBottomSheetDialog = new BottomDialog(that);
View specView = initSpecView();// 这里只是用inflater将一个布局填充为了一View对象
.setContentView(specView);
}
mBottomSheetDialog .show(); // 调用show方法就能展示了
mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mBottomSheetDialog = null;
}
});
private View initSpecView() {
View view = getLayoutInflater().inflate(R.layout.window_goods_choose,null);
......// 这里就是一些findViewById和对View 进行设置的操作
}
2、View的形式
对于控件的控制和其他CoordinatorLayout的子类一样,通过Beavior来实现
View bottomSheet = findViewById(R.id.bottom_sheet);//这里需要注意的是:view必须是CoordinatorLayout的子类
behavior = BottomSheetBehavior.from(bottomSheet);//该View需要配置Hehavior属性,Android 的默认实现为:app:layout_behavior="@string/bottom_sheet_behavior"
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {//设置回调
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
// React to state change
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
// React to dragging events
}
});
3、对状态的控制
通过Beavhior对象调用,setState方法,比如:
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);// 展开
- behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); //关闭
默认Beavhior里面的集中状态介绍:
STATE_COLLAPSED: 收缩,只会在底部显示该layout的一部分。要显示的部分的高度由 pp:behavior_peekHeight属性控制,默认下是0。代码中由BottomSheetBehavior#setPeekHeight()控制。
STATE_DRAGGING: 手指拖动着滑动状态。
STATE_SETTLING: 由用户放开手指到layout最终停止这中间的状态。
STATE_EXPANDED: 完全展开的状态。如果bottom sheet高度过高,则完全展开并可以滑动;否则,就完全展开,并上部留有剩余空间。
STATE_HIDDEN: 隐藏状态。
三、需要注意的问题(遇到的坑)
1、默认打开Dialog的时候,其实Dialog并没有完全打开至
STATE_EXPANDED状态,默认是在
STATE_COLLAPSED的状态,这个对于状态的控制,上述已经有说明,具体根据自己的需求去改变即可
2、通过show()方法展示出来的B
ottomSheetDialog,如果用户是通过向下滑动的方式隐藏的话,下次再展示的时候,会发现无法展示了,仅仅背景变暗而已。这个也是由于其状态的问题造成的,因为我们在下滑隐藏
B
ottomSheetDialog的时候,其状态变为了
STATE_HIDDEN,但是
其并没有销毁,重新show()的时候,自然也不会重建,但是状态一直处于
STATE_HIDDEN,所以自然就无法展示了。解决方案的话也有两种方式:
(1)每次需要展示的时候,都全部直接重新new一个对象出来
(2)通过反射改变源码,让其每次在show()的时候,将状态改为
STATE_EXPANDED。代码如下:
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatDialog;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import java.lang.reflect.Field;
/**
* Base class for {@link android.app.Dialog}s styled as a bottom sheet.
*/
public class MyDialog extends AppCompatDialog {
private BottomSheetBehavior bottomSheetBehavior;
public MyDialog(@NonNull Context context) {
this(context, 0);
}
public MyDialog(@NonNull Context context, @StyleRes int theme) {
super(context, getThemeResId(context, theme));
// We hide the title bar for any style configuration. Otherwise, there will be a gap
// above the bottom sheet when it is expanded.
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
}
protected MyDialog(@NonNull Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
}
@Override
public void setContentView(@LayoutRes int layoutResId) {
super.setContentView(wrapInBottomSheet(layoutResId, null, null));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setLayout(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
@Override
public void setContentView(View view) {
super.setContentView(wrapInBottomSheet(0, view, null));
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
super.setContentView(wrapInBottomSheet(0, view, params));
}
private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) {
final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),
android.support.design.R.layout.design_bottom_sheet_dialog, null);
if (layoutResId != 0 && view == null) {
view = getLayoutInflater().inflate(layoutResId, coordinator, false);
}
FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(android.support.design.R.id.design_bottom_sheet);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setBottomSheetCallback(mBottomSheetCallback);
if (params == null) {
bottomSheet.addView(view);
} else {
bottomSheet.addView(view, params);
}
// We treat the CoordinatorLayout as outside the dialog though it is technically inside
if (shouldWindowCloseOnTouchOutside()) {
coordinator.findViewById(android.support.design.R.id.touch_outside).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isShowing()) {
cancel();
}
}
});
}
return coordinator;
}
private boolean shouldWindowCloseOnTouchOutside() {
if (Build.VERSION.SDK_INT < 11) {
return true;
}
TypedValue value = new TypedValue();
//noinspection SimplifiableIfStatement
if (getContext().getTheme()
.resolveAttribute(android.R.attr.windowCloseOnTouchOutside, value, true)) {
return value.data != 0;
}
return false;
}
private static int getThemeResId(Context context, int themeId) {
if (themeId == 0) {
// If the provided theme is 0, then retrieve the dialogTheme from our theme
TypedValue outValue = new TypedValue();
if (context.getTheme().resolveAttribute(
android.support.design.R.attr.bottomSheetDialogTheme, outValue, true)) {
themeId = outValue.resourceId;
} else {
// bottomSheetDialogTheme is not provided; we default to our light theme
themeId = android.support.design.R.style.Theme_Design_Light_BottomSheetDialog;
}
}
return themeId;
}
private BottomSheetBehavior.BottomSheetCallback mBottomSheetCallback
= new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet,
@BottomSheetBehavior.State int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
};
@Override
protected void onStart() {
super.onStart();
// 会有异常
// if (bottomSheetBehavior != null)
// bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
//暂时解决方法
try {
Field field = BottomSheetBehavior.class.getDeclaredField("mState");
field.setAccessible(true);
field.setInt(bottomSheetBehavior, BottomSheetBehavior.STATE_EXPANDED);
} catch (NoSuchFieldException e) {
Log.e("BottomSheetBehavior", "Error setting BottomSheetBehavior initial state (1)", e);
} catch (IllegalAccessException e) {
Log.e("BottomSheetBehavior", "Error setting BottomSheetBehavior initial state (2)", e);
}
}
}
该种实现方式转自:http://blog.csdn.net/sunshine2050_csdn/article/details/50818197,非常感谢其贡献。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)