PopWindowUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package com.edufound.reader.popwindow;
  2. import android.content.Context;
  3. import android.view.Gravity;
  4. import android.view.KeyEvent;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.widget.FrameLayout;
  8. import android.widget.ImageView;
  9. import android.widget.PopupWindow;
  10. import com.edufound.reader.R;
  11. import com.edufound.reader.application.EApplication;
  12. import com.edufound.reader.bean.RecordResultBean;
  13. import com.edufound.reader.cusview.GridRadioGroup;
  14. import com.edufound.reader.listener.PopUtilClickListener;
  15. import com.edufound.reader.listener.PopupRecordStatusListener;
  16. import com.edufound.reader.presenter.PopWindowPresneter;
  17. import com.jakewharton.rxbinding4.view.RxView;
  18. import com.orhanobut.logger.Logger;
  19. import java.util.concurrent.TimeUnit;
  20. import io.reactivex.rxjava3.functions.Consumer;
  21. public class PopWindowUtil {
  22. private static PopWindowPresneter mPresenter;
  23. private static PopupWindow mPopupWindow;
  24. private static void initPresenter() {
  25. if (mPresenter == null) {
  26. mPresenter = new PopWindowPresneter();
  27. }
  28. }
  29. private static boolean checkWindowShoing() {
  30. if (mPopupWindow == null) {
  31. return false;
  32. }
  33. return mPopupWindow.isShowing();
  34. }
  35. public static void hidePopupWindow() {
  36. if (mPopupWindow.isShowing()) {
  37. mPopupWindow.dismiss();
  38. }
  39. }
  40. public static void showExitAppWindow(Context context, View parent) {
  41. initPresenter();
  42. if (checkWindowShoing()) {
  43. return;
  44. }
  45. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_exit_app, null);
  46. dialog_view.setFocusable(true);
  47. ImageView image = dialog_view.findViewById(R.id.popupwindow_exit_app_image);
  48. FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_exit_app_ok);
  49. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_exit_app_cancel);
  50. setClickListener(exit, o -> {
  51. EApplication.killAppProcess(context);
  52. });
  53. setClickListener(cancel, o -> {
  54. mPopupWindow.dismiss();
  55. });
  56. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  57. mPopupWindow.setFocusable(true);
  58. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  59. }
  60. public static void showSelectGradeWindow(Context context, View parent, int checked, PopUtilClickListener listener) {
  61. initPresenter();
  62. if (checkWindowShoing()) {
  63. return;
  64. }
  65. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_select_grade, null);
  66. dialog_view.setFocusable(true);
  67. FrameLayout btn_ok = dialog_view.findViewById(R.id.popupwindow_select_ok);
  68. GridRadioGroup gridRadioGroup = dialog_view.findViewById(R.id.popupwindow_select_grade_gridgroup);
  69. int id = 0;
  70. final String[] checkText = {String.valueOf(id)};
  71. switch (checked) {
  72. case 0:
  73. id = R.id.popupwindow_select_grade_one;
  74. checkText[0] = "一年级";
  75. break;
  76. case 1:
  77. id = R.id.popupwindow_select_grade_two;
  78. checkText[0] = "二年级";
  79. break;
  80. case 2:
  81. id = R.id.popupwindow_select_grade_three;
  82. checkText[0] = "三年级";
  83. break;
  84. case 3:
  85. id = R.id.popupwindow_select_grade_four;
  86. checkText[0] = "四年级";
  87. break;
  88. case 4:
  89. id = R.id.popupwindow_select_grade_pre;
  90. checkText[0] = "学前";
  91. break;
  92. }
  93. gridRadioGroup.setOnCheckedChangeListener(new GridRadioGroup.OnCheckedChangeListener() {
  94. @Override
  95. public void onCheckedChanged(GridRadioGroup group, int checkedId) {
  96. Logger.e("checkedId:" + checkedId);
  97. group.check(checkedId);
  98. switch (checkedId) {
  99. case R.id.popupwindow_select_grade_one:
  100. //一年级
  101. checkText[0] = "一年级";
  102. break;
  103. case R.id.popupwindow_select_grade_two:
  104. checkText[0] = "二年级";
  105. //二年级
  106. break;
  107. case R.id.popupwindow_select_grade_three:
  108. checkText[0] = "三年级";
  109. //三年级
  110. break;
  111. case R.id.popupwindow_select_grade_four:
  112. checkText[0] = "四年级";
  113. //四年级
  114. break;
  115. case R.id.popupwindow_select_grade_pre:
  116. //学前
  117. checkText[0] = "学前";
  118. break;
  119. }
  120. }
  121. });
  122. gridRadioGroup.check(id);
  123. setClickListener(btn_ok, o -> {
  124. mPopupWindow.dismiss();
  125. listener.clickSubmit(checkText[0]);
  126. });
  127. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  128. mPopupWindow.setFocusable(true);
  129. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  130. }
  131. public static void showExitLoginWindow(Context context, View parent, PopUtilClickListener listener) {
  132. initPresenter();
  133. if (checkWindowShoing()) {
  134. return;
  135. }
  136. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_exit_login, null);
  137. dialog_view.setFocusable(true);
  138. ImageView image = dialog_view.findViewById(R.id.popupwindow_exit_login_image);
  139. FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_exit_login_ok);
  140. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_exit_login_cancel);
  141. setClickListener(exit, o -> {
  142. listener.clickSubmit(null);
  143. });
  144. setClickListener(cancel, o -> {
  145. mPopupWindow.dismiss();
  146. listener.clickCancel();
  147. });
  148. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  149. mPopupWindow.setFocusable(true);
  150. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  151. }
  152. public static void showCancellationAccountWindow(Context context, View parent, PopUtilClickListener listener) {
  153. initPresenter();
  154. if (checkWindowShoing()) {
  155. return;
  156. }
  157. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_cancellation_account, null);
  158. dialog_view.setFocusable(true);
  159. FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_cancellation_account_ok);
  160. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_cancellation_account_cancel);
  161. setClickListener(exit, o -> {
  162. listener.clickSubmit(null);
  163. });
  164. setClickListener(cancel, o -> {
  165. mPopupWindow.dismiss();
  166. listener.clickCancel();
  167. });
  168. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  169. mPopupWindow.setFocusable(true);
  170. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  171. }
  172. public static void showBindWeChatWindow(Context context, View parent, PopUtilClickListener listener) {
  173. initPresenter();
  174. if (checkWindowShoing()) {
  175. return;
  176. }
  177. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_bind_wechat, null);
  178. dialog_view.setFocusable(true);
  179. ImageView back = dialog_view.findViewById(R.id.popupwindow_bindwechat_back);
  180. setClickListener(back, o -> {
  181. mPopupWindow.dismiss();
  182. listener.clickCancel();
  183. });
  184. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  185. mPopupWindow.setFocusable(true);
  186. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  187. }
  188. public static void showRecordStatusWindow(Context context, View parent, RecordResultBean bean, PopupRecordStatusListener listener) {
  189. initPresenter();
  190. if (checkWindowShoing()) {
  191. return;
  192. }
  193. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_record_status, null);
  194. dialog_view.setFocusable(true);
  195. dialog_view.setOnKeyListener(new View.OnKeyListener() {
  196. @Override
  197. public boolean onKey(View v, int keyCode, KeyEvent event) {
  198. if (keyCode == KeyEvent.KEYCODE_BACK) {
  199. return true;
  200. }
  201. return false;
  202. }
  203. });
  204. ImageView back = dialog_view.findViewById(R.id.popupwindow_record_status_close);
  205. setClickListener(back, o -> {
  206. mPopupWindow.dismiss();
  207. });
  208. mPresenter.initRecordStatusWindow(context, dialog_view, bean, listener);
  209. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  210. mPopupWindow.setFocusable(false);
  211. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  212. }
  213. public static void showMyOrderWindow(Context context, View parent) {
  214. initPresenter();
  215. if (checkWindowShoing()) {
  216. return;
  217. }
  218. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_myorder, null);
  219. dialog_view.setFocusable(true);
  220. dialog_view.setOnKeyListener(new View.OnKeyListener() {
  221. @Override
  222. public boolean onKey(View v, int keyCode, KeyEvent event) {
  223. if (keyCode == KeyEvent.KEYCODE_BACK) {
  224. return true;
  225. }
  226. return false;
  227. }
  228. });
  229. ImageView back = dialog_view.findViewById(R.id.popupwindow_myorder_back);
  230. setClickListener(back, o -> {
  231. mPopupWindow.dismiss();
  232. });
  233. mPresenter.myOrderWindowInit(context, dialog_view);
  234. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  235. mPopupWindow.setFocusable(false);
  236. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  237. }
  238. public static void showRecordRemovedWindow(Context context, View parent, PopUtilClickListener listener) {
  239. initPresenter();
  240. if (checkWindowShoing()) {
  241. return;
  242. }
  243. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_record_remove, null);
  244. dialog_view.setFocusable(true);
  245. FrameLayout ok = dialog_view.findViewById(R.id.popupwindow_record_remove_ok);
  246. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_record_remove_cancel);
  247. setClickListener(ok, o -> {
  248. mPopupWindow.dismiss();
  249. listener.clickSubmit(null);
  250. });
  251. setClickListener(cancel, o -> {
  252. mPopupWindow.dismiss();
  253. listener.clickCancel();
  254. });
  255. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  256. mPopupWindow.setFocusable(true);
  257. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  258. }
  259. private static void setClickListener(View view, Consumer onNext) {
  260. RxView.clicks(view).throttleFirst(2, TimeUnit.SECONDS).subscribe(onNext);
  261. }
  262. }