gpt4 book ai didi

Android:无法接收 BottomSheetDialog 状态更改回调

转载 作者:行者123 更新时间:2023-11-29 01:11:57 25 4
gpt4 key购买 nike

我已经像这样实现了 BottomSheetDialog:

addLinkSheet = new BottomSheetDialog(this);
addLinkSheetView = getLayoutInflater().inflate(R.layout.dialog_quickadd_wagrouplink, null);
addLinkSheet.setContentView(addLinkSheetView);
addLinkSheet.show();

我正在尝试使用此代码收听 Bottom Sheet (展开/折叠/隐藏)的状态变化:

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 被更改。我做错了吗?

最佳答案

您必须将回调包装在 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();

关闭 Bottom Sheet 对话框;你将需要:

bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

这将再次触发 onStateChanged 事件并关闭对话框。

关于Android:无法接收 BottomSheetDialog 状态更改回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42330317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com