通过向下拖动关闭后BottomSheetDialog保持隐藏 [英] BottomSheetDialog remains hidden after dismiss by dragging down

查看:45
本文介绍了通过向下拖动关闭后BottomSheetDialog保持隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am pretty curious about the behavior of the BottomSheetDialog when it is dismissed : when the user draggs it down to hide it, it will remain hidden, even if bottomSheetDialog#show() is called after. This only happens when it is dragged down, not when the user touches outside or when bottomSheetDialog#dismiss() is called programatically.

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

It is really annoying because I have a pretty big bottomSheetDialog with a recyclerview inside, and I have to create a new one every time I want to show the bottomSheetDialog.

所以,而不是仅仅这样做:

So instead of just doing this :

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

我每次都必须创建一个.

I have to create one every time.

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

Am I missing something or is it the normal behavior ? (Btw I use appcompat-v7:23.2.1)

推荐答案

所以我最终通过直接查看 BottomSheetDialog 实现设法解决了这个问题,我发现它不过是一个简单的Dialog 包装成一个常规的 BottomSheet.
问题出在 BottomSheetCallBack 中:

So I finally managed to solve this problem by looking directly into the BottomSheetDialog implementation, and I discovered it was nothing but a simple Dialog wrapped into a regular BottomSheet.
The problem was in the BottomSheetCallBack :

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

当对话框被下拉关闭时,会出现 HIDDEN 状态时出现问题.之后,即使 bottomSheetDialog.show() 被调用,对话框也会保持隐藏状态.我发现的最简单的修复方法是删除此状态并将其替换为 COLLAPSED 状态.

The problem occurs when the state HIDDEN is reached which happens when the dialog is dismissed by being dragged down. After that the dialog stays hidden even if bottomSheetDialog.show() is called. The most simple fix I found was to remove this state and replace it by the COLLAPSED state.

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

I create a classCustomBottomSheetDialog, copied the entire BottomSheetDialog class and added a single line to fix the problem :

@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) {
        }
    };

}

这篇关于通过向下拖动关闭后BottomSheetDialog保持隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆