BottomSheetDemo
#a bottomsheet widget that can modify height and drag state
#Code:
public abstract class BaseBottomSheetFrag extends BottomSheetDialogFragment {
protected Context mContext;
protected View rootView;
protected BottomSheetDialog dialog;
protected BottomSheetBehavior mBehavior;
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback
= new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
//禁止拖拽,
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
//设置为收缩状态
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
};
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
}
@Override
public void onStart() {
super.onStart();
mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
@Override
public void onDestroy() {
super.onDestroy();
//解除缓存View和当前ViewGroup的关联
((ViewGroup) (rootView.getParent())).removeView(rootView);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//每次打开都调用该方法 类似于onCreateView 用于返回一个Dialog实例
dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
if (rootView == null) {
//缓存下来的View 当为空时才需要初始化 并缓存
rootView = View.inflate(mContext, getLayoutResId(), null);
initView();
}
resetView();
//设置View重新关联
dialog.setContentView(rootView);
mBehavior = BottomSheetBehavior.from((View) rootView.getParent());
mBehavior.setSkipCollapsed(true);
mBehavior.setHideable(true);
mBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
//圆角边的关键(设置背景透明)
//((View) rootView.getParent()).setBackgroundColor(Color.TRANSPARENT);
//重置高度
if (dialog != null) {
View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
bottomSheet.getLayoutParams().height = ScreenUtils.getScreenHeight() * 2 / 3;
}
rootView.post(() ->{
mBehavior.setPeekHeight(rootView.getHeight());
});
return dialog;
}
public abstract int getLayoutResId();
/**
* 初始化View和设置数据等操作的方法
*/
public abstract void initView();
/**
* 重置的View和数据的空方法 子类可以选择实现
* 为避免多次inflate 父类缓存rootView
* 所以不会每次打开都调用{@link #initView()}方法
* 但是每次都会调用该方法 给子类能够重置View和数据
*/
public void resetView() {
}
public boolean isShowing() {
return dialog != null && dialog.isShowing();
}
/**
* 使用关闭弹框 是否使用动画可选
* 使用动画 同时切换界面Aty会卡顿 建议直接关闭
*
* @param isAnimation
*/
public void close(boolean isAnimation) {
if (isAnimation) {
if (mBehavior != null)
mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
} else {
dismiss();
}
}
}