通过向下拖动关闭后,BottomSheetDialog 保持隐藏状态

2023-12-28

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

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

因此,不要只这样做:

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

我每次都必须创建一个。

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


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

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

当达到隐藏状态时会出现问题,当对话框被向下拖动而关闭时会发生这种情况。之后,即使对话框保持隐藏状态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) {
        }
    };

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过向下拖动关闭后,BottomSheetDialog 保持隐藏状态 的相关文章

  • onActivityResult 中的完成活动不起作用

    我有一对必须同生共死的活动 基本上 AlphaActivity 会做一些工作 然后发送一个意图 startActivityForResult 对于 Beta 活动 当 Beta 活动完成时 我希望它发送一个意图 startActivity
  • android - EditText 打字速度很慢

    我的 EditText 在打字时响应速度很慢 这种滞后现象足以让我找到解决方案 我做了一些研究 发现了一个 SO 线程输入文本时 EditText 滞后 https stackoverflow com questions 6173591 a
  • ImageView 中的全尺寸图像

    我正在尝试在 ImageView 中绘制图像 但我希望它不缩放 并根据需要使用滚动条 我怎样才能做到这一点 现在我只有一个可绘制集作为 XML 中 ImageView 的 android src 这会自动缩放图像以适应屏幕宽度 我读到这可能
  • Android studio 在日志猫中“清除全部”,更改日志级别过滤器时日志仍然会回来

    在 LogCat 中 当我单击 全部清除 按钮时 它似乎清除了所有日志 但是 如果我更改日志级别并返回到之前的日志级别 则所有日志都会返回 例如 我正在查看 Verbose 我选择 全部清除 日志清除 我切换到 调试 我切换回 详细 现在所
  • 在 Android 中的活动、服务和应用程序之间传递变量

    有人可以给我提供以下活动 服务 应用程序组合的示例吗 我拥有这三个 但我已经把我的应用程序弄得一团糟 试图在这个地方传递一堆变量 现在我不知道发生了什么 请注意 我是 Android 新手 最近我一直在努力解决这个问题 因为有很多方法可以实
  • MI设备中即使应用程序被杀死,如何运行后台服务

    您好 我正在使用 alaram 管理器运行后台服务 它工作正常 但对于某些 mi 设备 后台服务无法工作 我使用了服务 但它无法工作 如何在 mi 中运行我的后台服务 MI UI有自己的安全选项 所以你需要的不仅仅是上面提到的粘性服务 你需
  • 将标题设置为操作栏时出现空指针异常

    Error Caused by java lang NullPointerException Attempt to invoke virtual method void android app ActionBar setTitle java
  • 如何将单选按钮状态保存到已保存/共享首选项?

    我可以在保存的首选项中保存字符串 但很难保存单选按钮 public class PersonalDetailsf extends Activity private SharedPreferences sharedPreferences pr
  • invalidateOptionsMenu 在片段中不起作用

    显示或隐藏项目ActionBar根据文本中是否有文本EditText or not 所以 我做了以下事情 public class NounSearch extends android app Fragment EditText seach
  • 当参数具有默认值时,为什么无法使用导航组件将参数传递给片段?

    我正在使用导航组件 但我不明白为什么如果定义了参数 则将参数传递给下面的方法时会出现错误 我正在使用 SafeArgs 只有当我为此参数定义默认值时才会出现此错误 有人可以解释一下为什么会发生这种情况以及如何解决它吗 这是导航图的部分代码
  • Facebook LoginActivity 未正确显示

    我有一个使用 Facebook 登录的应用程序 我有 FacebookSDK 并且使用 com facebook LoginActivity 问题是 在 10 英寸平板电脑上 当显示软键盘时 活动无法正确显示 我使用的是 Samsung G
  • 有没有办法在Android上创建一个三角形按钮?

    有没有办法创建一个三角形的按钮 我知道我可以将三角形图像作为背景 但这将使三角形之外的区域可单击 有没有办法固定按钮角 X 和 Y 以便我可以将其变成三角形 您可以覆盖OnTouch http developer android com r
  • Android:如何监控WiFi信号强度

    当信号强度发生变化时我会收到通知 我尝试创建以下方法并在 onCreate 中调用它 private void initializeWiFiListener Log i TAG executing initializeWiFiListene
  • Android:如何使视图增长以填充可用空间?

    这看起来很简单 但我不知道该怎么做 我有一个带有 EditText 和两个 ImageButtons 的水平布局 我希望 ImageButtons 具有固定大小 并且 EditText 占据布局中的剩余空间 如何才能做到这一点
  • 如何在android sdk上使用PowerMock

    我想为我的 android 项目编写一些单元测试和仪器测试 然而 我遇到了一个困扰我一段时间的问题 我需要模拟静态方法并伪造返回值来测试项目 经过一些论坛的调查 唯一的方法是使用PowerMock来模拟静态方法 这是我的 gradle 的一
  • 如何使 tablayout 文本大小相等?

    这就是我所做的 我为文本创建了一种样式
  • ormlite 将日期读取为 'yyyy-MM-dd'

    我需要读取给我的 sqlite 数据库 因此我无法更改表中的日期格式 yyyy MM dd 当我尝试使用 ormlite 为我生成对象时 使用以下注释 DatabaseField columnName REVISION DATE dataT
  • OpenGL ES 2.0 屏幕闪烁

    我面临着一个大问题 我正在使用带有 Android 4 0 3 的 Transformer tf101 选项卡 我的应用程序使用自定义 OpenGL ES 2 0 表面 我正在用纹理渲染多个平面 该纹理大约发生变化 每秒 20 次 并通过传
  • WebView 在某些设备上如果不长按则不会滚动

    我有一个 WebView 设置如下 the web view mWebView WebView findViewById R id webView push the url on to the web view mWebView loadU
  • Android应用程序kill事件捕获

    我想在我的应用程序被终止时执行一些操作 可以使用哪种方法来实现此目的 我正在开发 Android 5 0 这个问题的关键在于 您必须了解您的申请是否可以收到任何 当您的应用程序在任何情况下被终止时的额外回调 下面的答案是由德文连线 http

随机推荐