BottomSheetDialog的使用及注意事项

本文介绍了BottomSheetDialog的基本概念和使用方法,包括如何以Dialog或View形式展示,如何控制其状态等。同时,针对使用过程中可能遇到的问题提供了相应的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、BottomSheetDialog简介

用途:底部弹起的view或dialog。
实现:其关键也是CoordinatorLayout与Behavior
要求: 采用View的形式展示的话,用于展示的View必须具备如下两个要求:
            1,View类必须支持嵌套滚动。
            2,View类必须是CoordinatorLayout的直接子类。

二、简单使用

1、Dialog的形式

  1. if (mBottomSheetDialog == null){
  2. mBottomSheetDialog = new BottomDialog(that);
  3. View specView = initSpecView();// 这里只是用inflater将一个布局填充为了一View对象
  4. .setContentView(specView);
  5. }
  6. mBottomSheetDialog .show(); // 调用show方法就能展示了
  7. mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
  8. @Override
  9. public void onDismiss(DialogInterface dialog) {
  10. mBottomSheetDialog = null;
  11. }
  12. });
  13. private View initSpecView() {
  14. View view = getLayoutInflater().inflate(R.layout.window_goods_choose,null);
  15. ......// 这里就是一些findViewById和对View 进行设置的操作
  16. }

2、View的形式

对于控件的控制和其他CoordinatorLayout的子类一样,通过Beavior来实现
  1. View bottomSheet = findViewById(R.id.bottom_sheet);//这里需要注意的是:view必须是CoordinatorLayout的子类
  2. behavior = BottomSheetBehavior.from(bottomSheet);//该View需要配置Hehavior属性,Android 的默认实现为:app:layout_behavior="@string/bottom_sheet_behavior"
  3. behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {//设置回调
  4. @Override
  5. public void onStateChanged(@NonNull View bottomSheet, int newState) {
  6. // React to state change
  7. }
  8. @Override
  9. public void onSlide(@NonNull View bottomSheet, float slideOffset) {
  10. // React to dragging events
  11. }
  12. });

3、对状态的控制

通过Beavhior对象调用,setState方法,比如:
  1. behavior.setState(BottomSheetBehavior.STATE_EXPANDED);// 展开
  2. behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); //关闭
默认Beavhior里面的集中状态介绍:
STATE_COLLAPSED: 收缩,只会在底部显示该layout的一部分。要显示的部分的高度由 pp:behavior_peekHeight属性控制,默认下是0。代码中由BottomSheetBehavior#setPeekHeight()控制。
STATE_DRAGGING: 手指拖动着滑动状态。
STATE_SETTLING: 由用户放开手指到layout最终停止这中间的状态。
STATE_EXPANDED: 完全展开的状态。如果bottom sheet高度过高,则完全展开并可以滑动;否则,就完全展开,并上部留有剩余空间。
STATE_HIDDEN: 隐藏状态。

三、需要注意的问题(遇到的坑)

1、默认打开Dialog的时候,其实Dialog并没有完全打开至STATE_EXPANDED状态,默认是在STATE_COLLAPSED的状态,这个对于状态的控制,上述已经有说明,具体根据自己的需求去改变即可
2、通过show()方法展示出来的BottomSheetDialog,如果用户是通过向下滑动的方式隐藏的话,下次再展示的时候,会发现无法展示了,仅仅背景变暗而已。这个也是由于其状态的问题造成的,因为我们在下滑隐藏BottomSheetDialog的时候,其状态变为了STATE_HIDDEN,但是其并没有销毁,重新show()的时候,自然也不会重建,但是状态一直处于STATE_HIDDEN,所以自然就无法展示了。解决方案的话也有两种方式:
(1)每次需要展示的时候,都全部直接重新new一个对象出来
(2)通过反射改变源码,让其每次在show()的时候,将状态改为STATE_EXPANDED。代码如下:
  1. import android.content.Context;
  2. import android.os.Build;
  3. import android.os.Bundle;
  4. import android.support.annotation.LayoutRes;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.StyleRes;
  7. import android.support.design.widget.BottomSheetBehavior;
  8. import android.support.design.widget.CoordinatorLayout;
  9. import android.support.v7.app.AppCompatDialog;
  10. import android.util.Log;
  11. import android.util.TypedValue;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.view.Window;
  15. import android.widget.FrameLayout;
  16. import java.lang.reflect.Field;
  17. /**
  18. * Base class for {@link android.app.Dialog}s styled as a bottom sheet.
  19. */
  20. public class MyDialog extends AppCompatDialog {
  21. private BottomSheetBehavior bottomSheetBehavior;
  22. public MyDialog(@NonNull Context context) {
  23. this(context, 0);
  24. }
  25. public MyDialog(@NonNull Context context, @StyleRes int theme) {
  26. super(context, getThemeResId(context, theme));
  27. // We hide the title bar for any style configuration. Otherwise, there will be a gap
  28. // above the bottom sheet when it is expanded.
  29. supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
  30. }
  31. protected MyDialog(@NonNull Context context, boolean cancelable,
  32. OnCancelListener cancelListener) {
  33. super(context, cancelable, cancelListener);
  34. supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
  35. }
  36. @Override
  37. public void setContentView(@LayoutRes int layoutResId) {
  38. super.setContentView(wrapInBottomSheet(layoutResId, null, null));
  39. }
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. getWindow().setLayout(
  44. ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  45. }
  46. @Override
  47. public void setContentView(View view) {
  48. super.setContentView(wrapInBottomSheet(0, view, null));
  49. }
  50. @Override
  51. public void setContentView(View view, ViewGroup.LayoutParams params) {
  52. super.setContentView(wrapInBottomSheet(0, view, params));
  53. }
  54. private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) {
  55. final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),
  56. android.support.design.R.layout.design_bottom_sheet_dialog, null);
  57. if (layoutResId != 0 && view == null) {
  58. view = getLayoutInflater().inflate(layoutResId, coordinator, false);
  59. }
  60. FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(android.support.design.R.id.design_bottom_sheet);
  61. bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
  62. bottomSheetBehavior.setBottomSheetCallback(mBottomSheetCallback);
  63. if (params == null) {
  64. bottomSheet.addView(view);
  65. } else {
  66. bottomSheet.addView(view, params);
  67. }
  68. // We treat the CoordinatorLayout as outside the dialog though it is technically inside
  69. if (shouldWindowCloseOnTouchOutside()) {
  70. coordinator.findViewById(android.support.design.R.id.touch_outside).setOnClickListener(
  71. new View.OnClickListener() {
  72. @Override
  73. public void onClick(View view) {
  74. if (isShowing()) {
  75. cancel();
  76. }
  77. }
  78. });
  79. }
  80. return coordinator;
  81. }
  82. private boolean shouldWindowCloseOnTouchOutside() {
  83. if (Build.VERSION.SDK_INT < 11) {
  84. return true;
  85. }
  86. TypedValue value = new TypedValue();
  87. //noinspection SimplifiableIfStatement
  88. if (getContext().getTheme()
  89. .resolveAttribute(android.R.attr.windowCloseOnTouchOutside, value, true)) {
  90. return value.data != 0;
  91. }
  92. return false;
  93. }
  94. private static int getThemeResId(Context context, int themeId) {
  95. if (themeId == 0) {
  96. // If the provided theme is 0, then retrieve the dialogTheme from our theme
  97. TypedValue outValue = new TypedValue();
  98. if (context.getTheme().resolveAttribute(
  99. android.support.design.R.attr.bottomSheetDialogTheme, outValue, true)) {
  100. themeId = outValue.resourceId;
  101. } else {
  102. // bottomSheetDialogTheme is not provided; we default to our light theme
  103. themeId = android.support.design.R.style.Theme_Design_Light_BottomSheetDialog;
  104. }
  105. }
  106. return themeId;
  107. }
  108. private BottomSheetBehavior.BottomSheetCallback mBottomSheetCallback
  109. = new BottomSheetBehavior.BottomSheetCallback() {
  110. @Override
  111. public void onStateChanged(@NonNull View bottomSheet,
  112. @BottomSheetBehavior.State int newState) {
  113. if (newState == BottomSheetBehavior.STATE_HIDDEN) {
  114. dismiss();
  115. }
  116. }
  117. @Override
  118. public void onSlide(@NonNull View bottomSheet, float slideOffset) {
  119. }
  120. };
  121. @Override
  122. protected void onStart() {
  123. super.onStart();
  124. // 会有异常
  125. // if (bottomSheetBehavior != null)
  126. // bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
  127. //暂时解决方法
  128. try {
  129. Field field = BottomSheetBehavior.class.getDeclaredField("mState");
  130. field.setAccessible(true);
  131. field.setInt(bottomSheetBehavior, BottomSheetBehavior.STATE_EXPANDED);
  132. } catch (NoSuchFieldException e) {
  133. Log.e("BottomSheetBehavior", "Error setting BottomSheetBehavior initial state (1)", e);
  134. } catch (IllegalAccessException e) {
  135. Log.e("BottomSheetBehavior", "Error setting BottomSheetBehavior initial state (2)", e);
  136. }
  137. }
  138. }
该种实现方式转自:http://blog.youkuaiyun.com/sunshine2050_csdn/article/details/50818197,非常感谢其贡献。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值