gpt4 book ai didi

android - BottomSheetDialog 在通过向下拖动关闭后仍然隐藏

转载 作者:可可西里 更新时间:2023-11-01 19:05:00 24 4
gpt4 key购买 nike

我很好奇 BottomSheetDialog 被关闭时的行为:当用户向下拖动它以隐藏它时,它会保持隐藏状态,即使 bottomSheetDialog#show() 之后调用。这仅在它被向下拖动时发生,而不是在用户触摸外部或以编程方式调用 bottomSheetDialog#dismiss() 时发生。

这真的很烦人,因为我有一个很大的 bottomSheetDialog,里面有一个 recyclerview,每次我想显示 bottomSheetDialog 时,我都必须创建一个新的。

所以不要只是这样做:

if(bottomSheetDialog != null){
bottomSheetDialog.show();
else{
createNewBottomSheetDialog();
}

我每次都必须创建一个。

我是不是遗漏了什么或者这是正常行为? (顺便说一句,我使用 appcompat-v7:23.2.1)

最佳答案

所以我最终通过直接查看 BottomSheetDialog 实现解决了这个问题,我发现它只不过是一个简单的 Dialog 包裹在一个普通的 中 Bottom Sheet .
问题出在 BottomSheetCallBack 中:

@Override
public void onStateChanged(@NonNull View bottomSheet,
@BottomSheetBehavior.State int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}

当达到 HIDDEN 状态时会出现问题,当通过向下拖动对话框被关闭时会发生这种情况。之后,即使调用 bottomSheetDialog.show() 对话框也会保持隐藏状态。我发现的最简单的修复方法是删除此状态并将其替换为 COLLAPSED 状态。

我创建了一个 classCustomBottomSheetDialog,复制了整个 BottomSheetDialog 类并添加了一行来解决问题:

@Override
public void onStateChanged(@NonNull View bottomSheet,
@BottomSheetBehavior.State int newState) {

if (newState == CustomBottomSheetBehavior.STATE_HIDDEN) {

dismiss();
bottomSheetBehavior.setState(CustomBottomSheetBehavior.STATE_COLLAPSED);
}
}

最终代码如下:

public class CustomBottomSheetDialog extends AppCompatDialog {

public CustomBottomSheetDialog (@NonNull Context context) {
this(context, 0);
}

public CustomBottomSheetDialog (@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 CustomBottomSheetDialog (@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(),
R.layout.design_bottom_sheet_dialog, null);
if (layoutResId != 0 && view == null) {
view = getLayoutInflater().inflate(layoutResId, coordinator, false);
}
FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).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(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(
R.attr.bottomSheetDialogTheme, outValue, true)) {
themeId = outValue.resourceId;
} else {
// bottomSheetDialogTheme is not provided; we default to our light theme
themeId = 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();
bottomSheetBehavior.setState(CustomBottomSheetBehavior.STATE_COLLAPSED);
}
}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
};

}

关于android - BottomSheetDialog 在通过向下拖动关闭后仍然隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38804646/

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