参考:
BottomSheet的用法
Material Design系列,Behavior之BottomSheetBehavior与BottomSheetDialog
方式一 :BottomSheetBehavior:
核心:
app:layout_behavior="@string/bottom_sheet_behavior"
// <string name="bottom_sheet_behavior" translatable="false">
// android.support.design.widget.BottomSheetBehavior
// </string>
使用:
在布局中添加behavior:
<RelativeLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:minHeight="100dp"
app:behavior_hideable="false"
app:behavior_peekHeight="56dp"
app:layout_behavior="@string/bottom_sheet_behavior">
//.......
</RelativeLayout>
注意:布局最外层必须是CoordinatorLayout
Behavior作为CoordinatorLayout的子View的LayoutParams,所以CoordinatorLayout是万万不能少的
初始化状态
View bottomSheet = findViewById(R.id.bottom_sheet);//设置了bottom_sheet_behavior的view
if (bottomSheet != null) {
BottomSheetBehavior<View> behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);//设置初始状态
}
切换状态
int state = behavior.getState();
if (state == BottomSheetBehavior.STATE_EXPANDED) {//张开
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);//收缩
} else if(state == BottomSheetBehavior.STATE_COLLAPSED){
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);//隐藏
}else if(state == BottomSheetBehavior.STATE_HIDDEN){
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
设置监听
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
//这里是bottomSheet 状态的改变回调
//newState 有以下状态 :
//展开 BottomSheetBehavior.STATE_EXPANDED
//收起 BottomSheetBehavior.STATE_COLLAPSED
//拖动 BottomSheetBehavior.STATE_DRAGGING
//下滑 BottomSheetBehavior.STATE_SETTLING
//隐藏 BottomSheetBehavior.STATE_HIDDEN
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
//这里是拖拽中的回调,slideOffset为0-1 完全收起为0 完全展开为1
}
});
方式二 : BottomSheetDialog
public class BottomSheetDialog extends AppCompatDialog {
public class AppCompatDialog extends Dialog implements AppCompatCallback {
public class Dialog implements DialogInterface, Window.Callback,
KeyEvent.Callback, OnCreateContextMenuListener, Window.OnWindowDismissedCallback {
显示:
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
View inflate = View.inflate(this, R.layout.bottomsheetdalig_layout, null);
View qq = inflate.findViewById(R.id.share_qq);
View wx = inflate.findViewById(R.id.share_wx);
View sina = inflate.findViewById(R.id.share_sina);
qq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(BottomSheetDialogActivity.this, "分享到qq", Toast.LENGTH_SHORT).show();
bottomSheetDialog.dismiss();
}
});
wx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(BottomSheetDialogActivity.this, "分享到wx", Toast.LENGTH_SHORT).show();
bottomSheetDialog.dismiss();
}
});
sina.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(BottomSheetDialogActivity.this, "分享到sina", Toast.LENGTH_SHORT).show();
bottomSheetDialog.dismiss();
}
});
bottomSheetDialog.setContentView(inflate);
bottomSheetDialog.show();
隐藏:
bottomSheetDialog.dismiss();
坑一:
使用BottomSheetDialog弹出时,状态栏变黑
解决:
/**
* 解决状态栏变黑的问题
*/
public class MyBottomSheetDialog extends BottomSheetDialog {
private final Activity mActivity;
public MyBottomSheetDialog(@NonNull Context context) {
super(context);
mActivity = (Activity) context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int screenHeight = getScreenHeight(mActivity);
int statusBarHeight = getStatusBarHeight(getContext());
int dialogHeight = screenHeight - statusBarHeight;
Window window = getWindow();
if (window != null) {
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, dialogHeight == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : dialogHeight);
}
}
private static int getScreenHeight(Activity activity) {
DisplayMetrics displaymetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return displaymetrics.heightPixels;
}
private static int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
Resources res = context.getResources();
int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = res.getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
}
使用:
MyBottomSheetDialog mBottomSheetDialog = new MyBottomSheetDialog(this);
View view = getLayoutInflater().inflate(R.layout.dialog_bottom_sheet, null);
mBottomSheetDialog.setContentView(view);
mBottomSheetDialog.setCancelable(true);
mBottomSheetDialog.setCanceledOnTouchOutside(true);
mBottomSheetDialog.show();