我已经像这样实现了BottomSheetDialog
:
addLinkSheet = new BottomSheetDialog(this);
addLinkSheetView = getLayoutInflater().inflate(R.layout.dialog_quickadd_wagrouplink, null);
addLinkSheet.setContentView(addLinkSheetView);
addLinkSheet.show();
我正在尝试使用以下代码来听取底部工作表(Expanded / Collapsed / Hidden)的状态更改:
BottomSheetBehavior behavior = BottomSheetBehavior.from((View)addLinkSheetView.getParent());
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
Toast.makeText(QuickAddWaGroup.this, "State changed", Toast.LENGTH_SHORT).show();
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
我的BottomSheet显示正常,但我从来没有看到状态的Toast
被改变。我做错了吗?
你必须将Callback包装在BottomSheetDialog的OnShowListener
中,它应该工作:
bottomSheetDialog = new BottomSheetDialog(this);
View contentView = getLayoutInflater().inflate(R.layout.bottom_content_view, null);
bottomSheetDialog.setContentView(contentView);
bottomSheetDialog.setCancelable(true);
bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
bottomSheetBehavior = BottomSheetBehavior.from((View)contentView.getParent());
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
Log.d(TAG, "State Expanded");
// ... do whatever is required on 'expanded'
} else if (newState == BottomSheetBehavior.STATE_HIDDEN) {
bottomSheetDialog.dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
if (Math.abs(slideOffset) > 0) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
}
}
});
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
// Get your contentView children set here...
bottomSheetDialog.show();
关闭bottomSheetdialog;你会需要:
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
这将再次触发onStateChanged事件并关闭对话框。
我最初试图在Fragment中覆盖onCreateView
方法来设置布局,但是没有通知Bottom Sheet状态更改。只有在我覆盖了BottomSheetDialogFragment中的setupDialog
方法而不是onCreateView
之后,才能听到状态更改。这是示例代码
public class AddMetricFragment extends BottomSheetDialogFragment {
BottomSheetBehavior.BottomSheetCallback bottomSheetCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
// React to state change
Log.d("logtag", "Bottom Sheet State " + newState);
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
// React to dragging events
}
};
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
contentView = View.inflate(getContext(), R.layout.bottom_sheet_layout, null);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(bottomSheetCallback);
}
}
}