BaseDialog.java 8.5 KB

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