BaseDialog.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package com.cooleshow.base.widgets;
  2. import android.app.Dialog;
  3. import android.content.res.Configuration;
  4. import android.os.Build;
  5. import android.os.Bundle;
  6. import android.view.Gravity;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.view.Window;
  11. import android.view.WindowManager;
  12. import androidx.annotation.LayoutRes;
  13. import androidx.annotation.NonNull;
  14. import androidx.annotation.Nullable;
  15. import androidx.annotation.StyleRes;
  16. import androidx.fragment.app.DialogFragment;
  17. import androidx.fragment.app.FragmentManager;
  18. import androidx.fragment.app.FragmentTransaction;
  19. import com.cooleshow.base.R;
  20. /**
  21. * Dialog通用样式
  22. */
  23. public abstract class BaseDialog extends DialogFragment {
  24. private static final String MARGIN = "margin";
  25. private static final String WIDTH = "width";
  26. private static final String HEIGHT = "height";
  27. private static final String DIM = "dim_amount";
  28. private static final String GRAVITY = "gravity";
  29. private static final String CANCEL = "out_cancel";
  30. private static final String THEME = "theme";
  31. private static final String ANIM = "anim_style";
  32. private static final String LAYOUT = "layout_id";
  33. private int margin;//左右边距
  34. private int width;//宽度
  35. private int height;//高度
  36. private float dimAmount = 0.5f;//灰度深浅
  37. private int gravity = Gravity.CENTER;//显示的位置
  38. private boolean outCancel = true;//是否点击外部取消
  39. @StyleRes
  40. protected int theme = R.style.BaseDialog; // dialog主题
  41. @StyleRes
  42. private int animStyle;
  43. @LayoutRes
  44. protected int layoutId;
  45. public abstract int intLayoutId();
  46. public abstract void convertView(ViewHolder holder, BaseDialog dialog);
  47. public int initTheme() {
  48. return theme;
  49. }
  50. @Override
  51. public void onCreate(@Nullable Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. setStyle(DialogFragment.STYLE_NO_TITLE, initTheme());
  54. //恢复保存的数据
  55. if (savedInstanceState != null) {
  56. margin = savedInstanceState.getInt(MARGIN);
  57. width = savedInstanceState.getInt(WIDTH);
  58. height = savedInstanceState.getInt(HEIGHT);
  59. dimAmount = savedInstanceState.getFloat(DIM);
  60. gravity = savedInstanceState.getInt(GRAVITY);
  61. outCancel = savedInstanceState.getBoolean(CANCEL);
  62. theme = savedInstanceState.getInt(THEME);
  63. animStyle = savedInstanceState.getInt(ANIM);
  64. layoutId = savedInstanceState.getInt(LAYOUT);
  65. }
  66. }
  67. private void hideNavigationBar(Window window) {
  68. int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  69. | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  70. | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  71. | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  72. | View.SYSTEM_UI_FLAG_IMMERSIVE
  73. | View.SYSTEM_UI_FLAG_FULLSCREEN;
  74. if (window == null) {
  75. return;
  76. }
  77. window.getDecorView().setSystemUiVisibility(uiOptions);
  78. }
  79. public void adjustFullScreen(Window window) {
  80. if (window == null) {
  81. return;
  82. }
  83. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  84. WindowManager.LayoutParams lp = window.getAttributes();
  85. lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
  86. window.setAttributes(lp);
  87. final View decorView = window.getDecorView();
  88. decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  89. | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
  90. }
  91. }
  92. @Nullable
  93. @Override
  94. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  95. layoutId = intLayoutId();
  96. View view = inflater.inflate(layoutId, container, false);
  97. convertView(ViewHolder.create(view), this);
  98. return view;
  99. }
  100. @Override
  101. public void onStart() {
  102. super.onStart();
  103. initParams();
  104. }
  105. /**
  106. * 屏幕旋转等导致DialogFragment销毁后重建时保存数据
  107. *
  108. * @param outState
  109. */
  110. @Override
  111. public void onSaveInstanceState(Bundle outState) {
  112. super.onSaveInstanceState(outState);
  113. outState.putInt(MARGIN, margin);
  114. outState.putInt(WIDTH, width);
  115. outState.putInt(HEIGHT, height);
  116. outState.putFloat(DIM, dimAmount);
  117. outState.putInt(GRAVITY, gravity);
  118. outState.putBoolean(CANCEL, outCancel);
  119. outState.putInt(THEME, theme);
  120. outState.putInt(ANIM, animStyle);
  121. outState.putInt(LAYOUT, layoutId);
  122. }
  123. @Override
  124. public void onConfigurationChanged(@NonNull Configuration newConfig) {
  125. super.onConfigurationChanged(newConfig);
  126. initParams();
  127. }
  128. @NonNull
  129. @Override
  130. public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
  131. Dialog dialog = new Dialog(requireContext(), getTheme());
  132. hideNavigationBar(dialog.getWindow());
  133. adjustFullScreen(dialog.getWindow());
  134. return dialog;
  135. }
  136. private void initParams() {
  137. Window window = getDialog().getWindow();
  138. if (window != null) {
  139. WindowManager.LayoutParams lp = window.getAttributes();
  140. //调节灰色背景透明度[0-1],默认0.5f
  141. lp.dimAmount = dimAmount;
  142. if (gravity != 0) {
  143. lp.gravity = gravity;
  144. }
  145. switch (gravity) {
  146. case Gravity.LEFT:
  147. case (Gravity.LEFT | Gravity.BOTTOM):
  148. case (Gravity.LEFT | Gravity.TOP):
  149. if (animStyle == 0) {
  150. animStyle = R.style.LeftAnimation;
  151. }
  152. break;
  153. case Gravity.TOP:
  154. if (animStyle == 0) {
  155. animStyle = R.style.TopAnimation2;
  156. }
  157. break;
  158. case Gravity.RIGHT:
  159. case (Gravity.RIGHT | Gravity.BOTTOM):
  160. case (Gravity.RIGHT | Gravity.TOP):
  161. if (animStyle == 0) {
  162. animStyle = R.style.RightAnimation;
  163. }
  164. break;
  165. case Gravity.BOTTOM:
  166. if (animStyle == 0) {
  167. animStyle = R.style.BottomAnimation;
  168. }
  169. break;
  170. default:
  171. break;
  172. }
  173. //设置dialog宽度
  174. if (width == 0) {
  175. lp.width = DensityUtil.getScreenWidth(getContext()) - 2 * DensityUtil.dp2px(getContext(), margin);
  176. } else if (width == -1) {
  177. lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
  178. } else {
  179. lp.width = DensityUtil.dp2px(getContext(), width);
  180. }
  181. //设置dialog高度
  182. if (height == 0) {
  183. lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
  184. }else if(height == -1){
  185. lp.height = WindowManager.LayoutParams.MATCH_PARENT;
  186. } else {
  187. lp.height = DensityUtil.dp2px(getContext(), height);
  188. }
  189. //设置dialog进入、退出的动画
  190. window.setWindowAnimations(animStyle);
  191. window.setAttributes(lp);
  192. }
  193. setCancelable(outCancel);
  194. }
  195. public BaseDialog setMargin(int margin) {
  196. this.margin = margin;
  197. return this;
  198. }
  199. public BaseDialog setWidth(int width) {
  200. this.width = width;
  201. return this;
  202. }
  203. public BaseDialog setHeight(int height) {
  204. this.height = height;
  205. return this;
  206. }
  207. public BaseDialog setDimAmount(float dimAmount) {
  208. this.dimAmount = dimAmount;
  209. return this;
  210. }
  211. public BaseDialog setGravity(int gravity) {
  212. this.gravity = gravity;
  213. return this;
  214. }
  215. public BaseDialog setOutCancel(boolean outCancel) {
  216. this.outCancel = outCancel;
  217. return this;
  218. }
  219. public BaseDialog setAnimStyle(@StyleRes int animStyle) {
  220. this.animStyle = animStyle;
  221. return this;
  222. }
  223. public BaseDialog show(FragmentManager manager) {
  224. FragmentTransaction ft = manager.beginTransaction();
  225. if (this.isAdded()) {
  226. ft.remove(this).commit();
  227. }
  228. ft.add(this, String.valueOf(System.currentTimeMillis()));
  229. ft.addToBackStack(null); // 为解决该问题 Can not perform this action after onSaveInstanceState
  230. ft.commitAllowingStateLoss();
  231. return this;
  232. }
  233. }