BottomMenu.java 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. package com.kongzue.dialogx.dialogs;
  2. import android.view.MotionEvent;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.AdapterView;
  6. import android.widget.BaseAdapter;
  7. import android.widget.RelativeLayout;
  8. import androidx.annotation.ColorInt;
  9. import androidx.annotation.ColorRes;
  10. import com.kongzue.dialogx.DialogX;
  11. import com.kongzue.dialogx.R;
  12. import com.kongzue.dialogx.interfaces.BottomMenuListViewTouchEvent;
  13. import com.kongzue.dialogx.interfaces.DialogLifecycleCallback;
  14. import com.kongzue.dialogx.interfaces.DialogXStyle;
  15. import com.kongzue.dialogx.interfaces.OnBackPressedListener;
  16. import com.kongzue.dialogx.interfaces.OnBindView;
  17. import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;
  18. import com.kongzue.dialogx.interfaces.OnIconChangeCallBack;
  19. import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;
  20. import com.kongzue.dialogx.interfaces.OnMenuItemSelectListener;
  21. import com.kongzue.dialogx.util.BottomMenuArrayAdapter;
  22. import com.kongzue.dialogx.util.TextInfo;
  23. import com.kongzue.dialogx.util.views.BottomDialogListView;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.List;
  27. import static android.view.View.OVER_SCROLL_NEVER;
  28. /**
  29. * @author: Kongzue
  30. * @github: https://github.com/kongzue/
  31. * @homepage: http://kongzue.com/
  32. * @mail: myzcxhh@live.cn
  33. * @createTime: 2020/10/6 23:48
  34. */
  35. public class BottomMenu extends BottomDialog {
  36. public enum SELECT_MODE {
  37. NONE,
  38. SINGLE,
  39. MULTIPLE
  40. }
  41. protected BottomMenu me = this;
  42. protected int selectionIndex = -1;
  43. protected SELECT_MODE selectMode = SELECT_MODE.NONE;
  44. protected ArrayList<Integer> selectionItems;
  45. protected OnMenuItemClickListener<BottomMenu> onMenuItemClickListener;
  46. public static BottomMenu build() {
  47. return new BottomMenu();
  48. }
  49. public static BottomMenu build(OnBindView<BottomDialog> onBindView) {
  50. return new BottomMenu().setCustomView(onBindView);
  51. }
  52. protected BottomMenu() {
  53. super();
  54. if (style.overrideBottomDialogRes() != null) {
  55. bottomDialogMaxHeight = style.overrideBottomDialogRes().overrideBottomDialogMaxHeight();
  56. }
  57. if (bottomDialogMaxHeight <= 1 && bottomDialogMaxHeight > 0f) {
  58. bottomDialogMaxHeight = (int) (getRootFrameLayout().getMeasuredHeight() * bottomDialogMaxHeight);
  59. }
  60. }
  61. private OnIconChangeCallBack<BottomMenu> onIconChangeCallBack;
  62. private BottomDialogListView listView;
  63. private BaseAdapter menuListAdapter;
  64. private List<CharSequence> menuList;
  65. public static BottomMenu show(List<CharSequence> menuList) {
  66. BottomMenu bottomMenu = new BottomMenu();
  67. bottomMenu.setMenuList(menuList);
  68. bottomMenu.show();
  69. return bottomMenu;
  70. }
  71. public static BottomMenu show(List<CharSequence> menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  72. BottomMenu bottomMenu = new BottomMenu();
  73. bottomMenu.setMenuList(menuList);
  74. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  75. bottomMenu.show();
  76. return bottomMenu;
  77. }
  78. public static BottomMenu showStringList(List<String> menuList) {
  79. BottomMenu bottomMenu = new BottomMenu();
  80. bottomMenu.setMenuStringList(menuList);
  81. bottomMenu.show();
  82. return bottomMenu;
  83. }
  84. public static BottomMenu showStringList(List<String> menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  85. BottomMenu bottomMenu = new BottomMenu();
  86. bottomMenu.setMenuStringList(menuList);
  87. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  88. bottomMenu.show();
  89. return bottomMenu;
  90. }
  91. public static BottomMenu show(String[] menuList) {
  92. BottomMenu bottomMenu = new BottomMenu();
  93. bottomMenu.setMenuList(menuList);
  94. bottomMenu.show();
  95. return bottomMenu;
  96. }
  97. public static BottomMenu show(String[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  98. BottomMenu bottomMenu = new BottomMenu();
  99. bottomMenu.setMenuList(menuList);
  100. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  101. bottomMenu.show();
  102. return bottomMenu;
  103. }
  104. public static BottomMenu show(CharSequence[] menuList) {
  105. BottomMenu bottomMenu = new BottomMenu();
  106. bottomMenu.setMenuList(menuList);
  107. bottomMenu.show();
  108. return bottomMenu;
  109. }
  110. public static BottomMenu show(CharSequence[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  111. BottomMenu bottomMenu = new BottomMenu();
  112. bottomMenu.setMenuList(menuList);
  113. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  114. bottomMenu.show();
  115. return bottomMenu;
  116. }
  117. public static BottomMenu show(CharSequence title, CharSequence message, List<CharSequence> menuList) {
  118. BottomMenu bottomMenu = new BottomMenu();
  119. bottomMenu.title = title;
  120. bottomMenu.message = message;
  121. bottomMenu.setMenuList(menuList);
  122. bottomMenu.show();
  123. return bottomMenu;
  124. }
  125. public static BottomMenu show(CharSequence title, CharSequence message, List<CharSequence> menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  126. BottomMenu bottomMenu = new BottomMenu();
  127. bottomMenu.title = title;
  128. bottomMenu.message = message;
  129. bottomMenu.setMenuList(menuList);
  130. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  131. bottomMenu.show();
  132. return bottomMenu;
  133. }
  134. public static BottomMenu show(CharSequence title, List<CharSequence> menuList) {
  135. BottomMenu bottomMenu = new BottomMenu();
  136. bottomMenu.title = title;
  137. bottomMenu.setMenuList(menuList);
  138. bottomMenu.show();
  139. return bottomMenu;
  140. }
  141. public static BottomMenu show(CharSequence title, List<CharSequence> menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  142. BottomMenu bottomMenu = new BottomMenu();
  143. bottomMenu.title = title;
  144. bottomMenu.setMenuList(menuList);
  145. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  146. bottomMenu.show();
  147. return bottomMenu;
  148. }
  149. public static BottomMenu showStringList(CharSequence title, CharSequence message, List<String> menuList) {
  150. BottomMenu bottomMenu = new BottomMenu();
  151. bottomMenu.title = title;
  152. bottomMenu.message = message;
  153. bottomMenu.setMenuStringList(menuList);
  154. bottomMenu.show();
  155. return bottomMenu;
  156. }
  157. public static BottomMenu showStringList(CharSequence title, CharSequence message, List<String> menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  158. BottomMenu bottomMenu = new BottomMenu();
  159. bottomMenu.title = title;
  160. bottomMenu.message = message;
  161. bottomMenu.setMenuStringList(menuList);
  162. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  163. bottomMenu.show();
  164. return bottomMenu;
  165. }
  166. public static BottomMenu show(CharSequence title, CharSequence message, String[] menuList) {
  167. BottomMenu bottomMenu = new BottomMenu();
  168. bottomMenu.title = title;
  169. bottomMenu.message = message;
  170. bottomMenu.setMenuList(menuList);
  171. bottomMenu.show();
  172. return bottomMenu;
  173. }
  174. public static BottomMenu show(CharSequence title, CharSequence message, String[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  175. BottomMenu bottomMenu = new BottomMenu();
  176. bottomMenu.title = title;
  177. bottomMenu.message = message;
  178. bottomMenu.setMenuList(menuList);
  179. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  180. bottomMenu.show();
  181. return bottomMenu;
  182. }
  183. public static BottomMenu show(CharSequence title, CharSequence message, CharSequence[] menuList) {
  184. BottomMenu bottomMenu = new BottomMenu();
  185. bottomMenu.title = title;
  186. bottomMenu.message = message;
  187. bottomMenu.setMenuList(menuList);
  188. bottomMenu.show();
  189. return bottomMenu;
  190. }
  191. public static BottomMenu show(CharSequence title, CharSequence message, CharSequence[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  192. BottomMenu bottomMenu = new BottomMenu();
  193. bottomMenu.title = title;
  194. bottomMenu.message = message;
  195. bottomMenu.setMenuList(menuList);
  196. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  197. bottomMenu.show();
  198. return bottomMenu;
  199. }
  200. public static BottomMenu show(String title, String message, List<CharSequence> menuList) {
  201. BottomMenu bottomMenu = new BottomMenu();
  202. bottomMenu.title = title;
  203. bottomMenu.message = message;
  204. bottomMenu.setMenuList(menuList);
  205. bottomMenu.show();
  206. return bottomMenu;
  207. }
  208. public static BottomMenu show(String title, String message, List<CharSequence> menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  209. BottomMenu bottomMenu = new BottomMenu();
  210. bottomMenu.title = title;
  211. bottomMenu.message = message;
  212. bottomMenu.setMenuList(menuList);
  213. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  214. bottomMenu.show();
  215. return bottomMenu;
  216. }
  217. public static BottomMenu showStringList(String title, String message, List<String> menuList) {
  218. BottomMenu bottomMenu = new BottomMenu();
  219. bottomMenu.title = title;
  220. bottomMenu.message = message;
  221. bottomMenu.setMenuStringList(menuList);
  222. bottomMenu.show();
  223. return bottomMenu;
  224. }
  225. public static BottomMenu showStringList(String title, String message, List<String> menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  226. BottomMenu bottomMenu = new BottomMenu();
  227. bottomMenu.title = title;
  228. bottomMenu.message = message;
  229. bottomMenu.setMenuStringList(menuList);
  230. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  231. bottomMenu.show();
  232. return bottomMenu;
  233. }
  234. public static BottomMenu show(String title, String message, String[] menuList) {
  235. BottomMenu bottomMenu = new BottomMenu();
  236. bottomMenu.title = title;
  237. bottomMenu.message = message;
  238. bottomMenu.setMenuList(menuList);
  239. bottomMenu.show();
  240. return bottomMenu;
  241. }
  242. public static BottomMenu show(String title, String message, String[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  243. BottomMenu bottomMenu = new BottomMenu();
  244. bottomMenu.title = title;
  245. bottomMenu.message = message;
  246. bottomMenu.setMenuList(menuList);
  247. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  248. bottomMenu.show();
  249. return bottomMenu;
  250. }
  251. public static BottomMenu show(String title, String message, CharSequence[] menuList) {
  252. BottomMenu bottomMenu = new BottomMenu();
  253. bottomMenu.title = title;
  254. bottomMenu.message = message;
  255. bottomMenu.setMenuList(menuList);
  256. bottomMenu.show();
  257. return bottomMenu;
  258. }
  259. public static BottomMenu show(String title, String message, CharSequence[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  260. BottomMenu bottomMenu = new BottomMenu();
  261. bottomMenu.title = title;
  262. bottomMenu.message = message;
  263. bottomMenu.setMenuList(menuList);
  264. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  265. bottomMenu.show();
  266. return bottomMenu;
  267. }
  268. public static BottomMenu show(int titleResId, int messageResId, List<CharSequence> menuList) {
  269. BottomMenu bottomMenu = new BottomMenu();
  270. bottomMenu.title = bottomMenu.getString(titleResId);
  271. bottomMenu.message = bottomMenu.getString(messageResId);
  272. bottomMenu.setMenuList(menuList);
  273. bottomMenu.show();
  274. return bottomMenu;
  275. }
  276. public static BottomMenu show(int titleResId, List<CharSequence> menuList) {
  277. BottomMenu bottomMenu = new BottomMenu();
  278. bottomMenu.title = bottomMenu.getString(titleResId);
  279. bottomMenu.setMenuList(menuList);
  280. bottomMenu.show();
  281. return bottomMenu;
  282. }
  283. public static BottomMenu showStringList(int titleResId, int messageResId, List<String> menuList) {
  284. BottomMenu bottomMenu = new BottomMenu();
  285. bottomMenu.title = bottomMenu.getString(titleResId);
  286. bottomMenu.message = bottomMenu.getString(messageResId);
  287. bottomMenu.setMenuStringList(menuList);
  288. bottomMenu.show();
  289. return bottomMenu;
  290. }
  291. public static BottomMenu show(int titleResId, int messageResId, String[] menuList) {
  292. BottomMenu bottomMenu = new BottomMenu();
  293. bottomMenu.title = bottomMenu.getString(titleResId);
  294. bottomMenu.message = bottomMenu.getString(messageResId);
  295. bottomMenu.setMenuList(menuList);
  296. bottomMenu.show();
  297. return bottomMenu;
  298. }
  299. public static BottomMenu show(int titleResId, int messageResId, CharSequence[] menuList) {
  300. BottomMenu bottomMenu = new BottomMenu();
  301. bottomMenu.title = bottomMenu.getString(titleResId);
  302. bottomMenu.message = bottomMenu.getString(messageResId);
  303. bottomMenu.setMenuList(menuList);
  304. bottomMenu.show();
  305. return bottomMenu;
  306. }
  307. public static BottomMenu show(int titleResId, int messageResId, List<CharSequence> menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  308. BottomMenu bottomMenu = new BottomMenu();
  309. bottomMenu.title = bottomMenu.getString(titleResId);
  310. bottomMenu.message = bottomMenu.getString(messageResId);
  311. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  312. bottomMenu.setMenuList(menuList);
  313. bottomMenu.show();
  314. return bottomMenu;
  315. }
  316. public static BottomMenu show(int titleResId, List<CharSequence> menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  317. BottomMenu bottomMenu = new BottomMenu();
  318. bottomMenu.title = bottomMenu.getString(titleResId);
  319. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  320. bottomMenu.setMenuList(menuList);
  321. bottomMenu.show();
  322. return bottomMenu;
  323. }
  324. public static BottomMenu showStringList(int titleResId, int messageResId, List<String> menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  325. BottomMenu bottomMenu = new BottomMenu();
  326. bottomMenu.title = bottomMenu.getString(titleResId);
  327. bottomMenu.message = bottomMenu.getString(messageResId);
  328. bottomMenu.setMenuStringList(menuList);
  329. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  330. bottomMenu.show();
  331. return bottomMenu;
  332. }
  333. public static BottomMenu show(int titleResId, int messageResId, String[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  334. BottomMenu bottomMenu = new BottomMenu();
  335. bottomMenu.title = bottomMenu.getString(titleResId);
  336. bottomMenu.message = bottomMenu.getString(messageResId);
  337. bottomMenu.setMenuList(menuList);
  338. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  339. bottomMenu.show();
  340. return bottomMenu;
  341. }
  342. public static BottomMenu show(int titleResId, int messageResId, CharSequence[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  343. BottomMenu bottomMenu = new BottomMenu();
  344. bottomMenu.title = bottomMenu.getString(titleResId);
  345. bottomMenu.message = bottomMenu.getString(messageResId);
  346. bottomMenu.setMenuList(menuList);
  347. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  348. bottomMenu.show();
  349. return bottomMenu;
  350. }
  351. public static BottomMenu show(CharSequence title, CharSequence[] menuList) {
  352. BottomMenu bottomMenu = new BottomMenu();
  353. bottomMenu.title = title;
  354. bottomMenu.setMenuList(menuList);
  355. bottomMenu.show();
  356. return bottomMenu;
  357. }
  358. public static BottomMenu show(CharSequence title, String[] menuList) {
  359. BottomMenu bottomMenu = new BottomMenu();
  360. bottomMenu.title = title;
  361. bottomMenu.setMenuList(menuList);
  362. bottomMenu.show();
  363. return bottomMenu;
  364. }
  365. public static BottomMenu show(CharSequence title, CharSequence[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  366. BottomMenu bottomMenu = new BottomMenu();
  367. bottomMenu.title = title;
  368. bottomMenu.setMenuList(menuList);
  369. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  370. bottomMenu.show();
  371. return bottomMenu;
  372. }
  373. public static BottomMenu show(CharSequence title, String[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  374. BottomMenu bottomMenu = new BottomMenu();
  375. bottomMenu.title = title;
  376. bottomMenu.setMenuList(menuList);
  377. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  378. bottomMenu.show();
  379. return bottomMenu;
  380. }
  381. public static BottomMenu show(int titleResId, CharSequence[] menuList) {
  382. BottomMenu bottomMenu = new BottomMenu();
  383. bottomMenu.title = bottomMenu.getString(titleResId);
  384. bottomMenu.setMenuList(menuList);
  385. bottomMenu.show();
  386. return bottomMenu;
  387. }
  388. public static BottomMenu show(int titleResId, String[] menuList) {
  389. BottomMenu bottomMenu = new BottomMenu();
  390. bottomMenu.title = bottomMenu.getString(titleResId);
  391. bottomMenu.setMenuList(menuList);
  392. bottomMenu.show();
  393. return bottomMenu;
  394. }
  395. public static BottomMenu show(int titleResId, CharSequence[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  396. BottomMenu bottomMenu = new BottomMenu();
  397. bottomMenu.title = bottomMenu.getString(titleResId);
  398. bottomMenu.setMenuList(menuList);
  399. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  400. bottomMenu.show();
  401. return bottomMenu;
  402. }
  403. public static BottomMenu show(int titleResId, String[] menuList, OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  404. BottomMenu bottomMenu = new BottomMenu();
  405. bottomMenu.title = bottomMenu.getString(titleResId);
  406. bottomMenu.setMenuList(menuList);
  407. bottomMenu.setOnMenuItemClickListener(onMenuItemClickListener);
  408. bottomMenu.show();
  409. return bottomMenu;
  410. }
  411. private float touchDownY;
  412. public static final int ITEM_CLICK_DELAY = 100;
  413. private long lastClickTime = 0;
  414. @Override
  415. protected void onDialogInit(final DialogImpl dialog) {
  416. if (dialog != null) {
  417. dialog.boxList.setVisibility(View.VISIBLE);
  418. if (!isAllowInterceptTouch()) {
  419. dialog.bkg.setMaxHeight((int) bottomDialogMaxHeight);
  420. if (bottomDialogMaxHeight != 0) {
  421. dialogImpl.scrollView.lockScroll(true);
  422. }
  423. }
  424. int dividerDrawableResId = 0;
  425. int dividerHeight = 1;
  426. if (style.overrideBottomDialogRes() != null) {
  427. dividerDrawableResId = style.overrideBottomDialogRes().overrideMenuDividerDrawableRes(isLightTheme());
  428. dividerHeight = style.overrideBottomDialogRes().overrideMenuDividerHeight(isLightTheme());
  429. }
  430. if (dividerDrawableResId == 0) {
  431. dividerDrawableResId = isLightTheme() ? R.drawable.rect_dialogx_material_menu_split_divider : R.drawable.rect_dialogx_material_menu_split_divider_night;
  432. }
  433. listView = new BottomDialogListView(dialog, getContext());
  434. listView.setOverScrollMode(OVER_SCROLL_NEVER);
  435. listView.setDivider(getResources().getDrawable(dividerDrawableResId));
  436. listView.setDividerHeight(dividerHeight);
  437. listView.setBottomMenuListViewTouchEvent(new BottomMenuListViewTouchEvent() {
  438. @Override
  439. public void down(MotionEvent event) {
  440. touchDownY = dialog.bkg.getY();
  441. }
  442. });
  443. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  444. @Override
  445. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  446. long currentTime = System.currentTimeMillis();
  447. if (currentTime - lastClickTime > ITEM_CLICK_DELAY) {
  448. lastClickTime = currentTime;
  449. float deltaY = Math.abs(touchDownY - dialog.bkg.getY());
  450. if (deltaY > dip2px(15)) {
  451. return;
  452. }
  453. switch (selectMode) {
  454. case NONE:
  455. if (onMenuItemClickListener != null) {
  456. if (!onMenuItemClickListener.onClick(me, menuList.get(position), position)) {
  457. dismiss();
  458. }
  459. } else {
  460. dismiss();
  461. }
  462. break;
  463. case SINGLE:
  464. if (onMenuItemClickListener instanceof OnMenuItemSelectListener) {
  465. OnMenuItemSelectListener<BottomMenu> onMenuItemSelectListener = (OnMenuItemSelectListener<BottomMenu>) onMenuItemClickListener;
  466. if (!onMenuItemSelectListener.onClick(me, menuList.get(position), position)) {
  467. dismiss();
  468. } else {
  469. selectionIndex = position;
  470. menuListAdapter.notifyDataSetInvalidated();
  471. menuListAdapter.notifyDataSetChanged();
  472. onMenuItemSelectListener.onOneItemSelect(me, menuList.get(position), position, true);
  473. }
  474. } else {
  475. if (onMenuItemClickListener != null) {
  476. if (!onMenuItemClickListener.onClick(me, menuList.get(position), position)) {
  477. dismiss();
  478. }
  479. } else {
  480. dismiss();
  481. }
  482. }
  483. break;
  484. case MULTIPLE:
  485. if (onMenuItemClickListener instanceof OnMenuItemSelectListener) {
  486. OnMenuItemSelectListener<BottomMenu> onMenuItemSelectListener = (OnMenuItemSelectListener<BottomMenu>) onMenuItemClickListener;
  487. if (!onMenuItemSelectListener.onClick(me, menuList.get(position), position)) {
  488. dismiss();
  489. } else {
  490. if (selectionItems.contains(position)) {
  491. selectionItems.remove(new Integer(position));
  492. } else {
  493. selectionItems.add(position);
  494. }
  495. menuListAdapter.notifyDataSetInvalidated();
  496. menuListAdapter.notifyDataSetChanged();
  497. int[] resultArray = new int[selectionItems.size()];
  498. CharSequence[] selectTextArray = new CharSequence[selectionItems.size()];
  499. for (int i = 0; i < selectionItems.size(); i++) {
  500. resultArray[i] = selectionItems.get(i);
  501. selectTextArray[i] = menuList.get(resultArray[i]);
  502. }
  503. onMenuItemSelectListener.onMultiItemSelect(me, selectTextArray, resultArray);
  504. }
  505. } else {
  506. if (onMenuItemClickListener != null) {
  507. if (!onMenuItemClickListener.onClick(me, menuList.get(position), position)) {
  508. dismiss();
  509. }
  510. } else {
  511. dismiss();
  512. }
  513. }
  514. break;
  515. }
  516. }
  517. }
  518. });
  519. if (style.overrideBottomDialogRes() != null) {
  520. if (style.overrideBottomDialogRes().overrideMenuItemLayout(true, 0, 0, false) != 0) {
  521. listView.setSelector(R.color.empty);
  522. }
  523. }
  524. RelativeLayout.LayoutParams listViewLp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  525. dialog.boxList.addView(listView, listViewLp);
  526. refreshUI();
  527. }
  528. }
  529. @Override
  530. public void refreshUI() {
  531. if (getDialogImpl() == null) return;
  532. if (listView != null) {
  533. if (menuListAdapter == null) {
  534. menuListAdapter = new BottomMenuArrayAdapter(me, getContext(), menuList);
  535. }
  536. if (listView.getAdapter() == null) {
  537. listView.setAdapter(menuListAdapter);
  538. } else {
  539. if (listView.getAdapter() != menuListAdapter) {
  540. listView.setAdapter(menuListAdapter);
  541. } else {
  542. menuListAdapter.notifyDataSetChanged();
  543. }
  544. }
  545. }
  546. super.refreshUI();
  547. }
  548. public void preRefreshUI() {
  549. if (getDialogImpl() == null) return;
  550. runOnMain(new Runnable() {
  551. @Override
  552. public void run() {
  553. refreshUI();
  554. }
  555. });
  556. }
  557. @Override
  558. public String dialogKey() {
  559. return getClass().getSimpleName() + "(" + Integer.toHexString(hashCode()) + ")";
  560. }
  561. public List<CharSequence> getMenuList() {
  562. return menuList;
  563. }
  564. public BottomMenu setMenuList(List<CharSequence> menuList) {
  565. this.menuList = menuList;
  566. this.menuListAdapter = null;
  567. preRefreshUI();
  568. return this;
  569. }
  570. private boolean isSameSize(int menuListSize) {
  571. if (this.menuList == null || this.menuList.size() == 0) {
  572. return true;
  573. }
  574. return this.menuList.size() == menuListSize;
  575. }
  576. public BottomMenu setMenuStringList(List<String> menuList) {
  577. this.menuList = new ArrayList<>();
  578. this.menuList.addAll(menuList);
  579. this.menuListAdapter = null;
  580. preRefreshUI();
  581. return this;
  582. }
  583. public BottomMenu setMenuList(String[] menuList) {
  584. this.menuList = new ArrayList<>();
  585. this.menuList.addAll(Arrays.asList(menuList));
  586. this.menuListAdapter = null;
  587. preRefreshUI();
  588. return this;
  589. }
  590. public BottomMenu setMenuList(CharSequence[] menuList) {
  591. this.menuList = Arrays.asList(menuList);
  592. this.menuListAdapter = null;
  593. preRefreshUI();
  594. return this;
  595. }
  596. public OnIconChangeCallBack<BottomMenu> getOnIconChangeCallBack() {
  597. return onIconChangeCallBack;
  598. }
  599. public BottomMenu setOnIconChangeCallBack(OnIconChangeCallBack<BottomMenu> onIconChangeCallBack) {
  600. this.onIconChangeCallBack = onIconChangeCallBack;
  601. return this;
  602. }
  603. public OnBackPressedListener getOnBackPressedListener() {
  604. return onBackPressedListener;
  605. }
  606. public BottomMenu setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
  607. this.onBackPressedListener = onBackPressedListener;
  608. preRefreshUI();
  609. return this;
  610. }
  611. public BottomMenu setDialogLifecycleCallback(DialogLifecycleCallback<BottomDialog> dialogLifecycleCallback) {
  612. this.dialogLifecycleCallback = dialogLifecycleCallback;
  613. if (isShow) dialogLifecycleCallback.onShow(me);
  614. return this;
  615. }
  616. public BottomMenu setStyle(DialogXStyle style) {
  617. this.style = style;
  618. return this;
  619. }
  620. public BottomMenu setTheme(DialogX.THEME theme) {
  621. this.theme = theme;
  622. return this;
  623. }
  624. public boolean isCancelable() {
  625. if (privateCancelable != null) {
  626. return privateCancelable == BOOLEAN.TRUE;
  627. }
  628. if (overrideCancelable != null) {
  629. return overrideCancelable == BOOLEAN.TRUE;
  630. }
  631. return cancelable;
  632. }
  633. public BottomMenu setCancelable(boolean cancelable) {
  634. this.privateCancelable = cancelable ? BOOLEAN.TRUE : BOOLEAN.FALSE;
  635. preRefreshUI();
  636. return this;
  637. }
  638. public DialogImpl getDialogImpl() {
  639. return dialogImpl;
  640. }
  641. public CharSequence getTitle() {
  642. return title;
  643. }
  644. public BottomMenu setTitle(CharSequence title) {
  645. this.title = title;
  646. preRefreshUI();
  647. return this;
  648. }
  649. public BottomMenu setTitle(int titleResId) {
  650. this.title = getString(titleResId);
  651. preRefreshUI();
  652. return this;
  653. }
  654. public CharSequence getMessage() {
  655. return message;
  656. }
  657. public BottomMenu setMessage(CharSequence message) {
  658. this.message = message;
  659. preRefreshUI();
  660. return this;
  661. }
  662. public BottomMenu setMessage(int messageResId) {
  663. this.message = getString(messageResId);
  664. preRefreshUI();
  665. return this;
  666. }
  667. public CharSequence getCancelButton() {
  668. return cancelText;
  669. }
  670. public BottomMenu setCancelButton(CharSequence cancelText) {
  671. this.cancelText = cancelText;
  672. preRefreshUI();
  673. return this;
  674. }
  675. public BottomMenu setCancelButton(int cancelTextResId) {
  676. this.cancelText = getString(cancelTextResId);
  677. preRefreshUI();
  678. return this;
  679. }
  680. public BottomMenu setCancelButton(OnDialogButtonClickListener cancelButtonClickListener) {
  681. this.cancelButtonClickListener = cancelButtonClickListener;
  682. return this;
  683. }
  684. public BottomMenu setCancelButton(CharSequence cancelText, OnDialogButtonClickListener cancelButtonClickListener) {
  685. this.cancelText = cancelText;
  686. this.cancelButtonClickListener = cancelButtonClickListener;
  687. preRefreshUI();
  688. return this;
  689. }
  690. public BottomMenu setCancelButton(int cancelTextResId, OnDialogButtonClickListener cancelButtonClickListener) {
  691. this.cancelText = getString(cancelTextResId);
  692. this.cancelButtonClickListener = cancelButtonClickListener;
  693. preRefreshUI();
  694. return this;
  695. }
  696. public BottomMenu setCustomView(OnBindView<BottomDialog> onBindView) {
  697. this.onBindView = onBindView;
  698. preRefreshUI();
  699. return this;
  700. }
  701. public View getCustomView() {
  702. if (onBindView == null) return null;
  703. return onBindView.getCustomView();
  704. }
  705. public BottomMenu removeCustomView() {
  706. this.onBindView.clean();
  707. preRefreshUI();
  708. return this;
  709. }
  710. public boolean isAllowInterceptTouch() {
  711. return super.isAllowInterceptTouch();
  712. }
  713. public BottomMenu setAllowInterceptTouch(boolean allowInterceptTouch) {
  714. this.allowInterceptTouch = allowInterceptTouch;
  715. preRefreshUI();
  716. return this;
  717. }
  718. public float getBottomDialogMaxHeight() {
  719. return bottomDialogMaxHeight;
  720. }
  721. public BottomMenu setBottomDialogMaxHeight(float bottomDialogMaxHeight) {
  722. this.bottomDialogMaxHeight = bottomDialogMaxHeight;
  723. return this;
  724. }
  725. public OnMenuItemClickListener<BottomMenu> getOnMenuItemClickListener() {
  726. return onMenuItemClickListener;
  727. }
  728. public BottomMenu setOnMenuItemClickListener(OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  729. this.onMenuItemClickListener = onMenuItemClickListener;
  730. return this;
  731. }
  732. public BaseAdapter getMenuListAdapter() {
  733. return menuListAdapter;
  734. }
  735. public BottomMenu setMenuListAdapter(BaseAdapter menuListAdapter) {
  736. this.menuListAdapter = menuListAdapter;
  737. return this;
  738. }
  739. public OnDialogButtonClickListener getCancelButtonClickListener() {
  740. return cancelButtonClickListener;
  741. }
  742. public BottomMenu setCancelButtonClickListener(OnDialogButtonClickListener cancelButtonClickListener) {
  743. this.cancelButtonClickListener = cancelButtonClickListener;
  744. return this;
  745. }
  746. public TextInfo getTitleTextInfo() {
  747. return titleTextInfo;
  748. }
  749. public BottomMenu setTitleTextInfo(TextInfo titleTextInfo) {
  750. this.titleTextInfo = titleTextInfo;
  751. preRefreshUI();
  752. return this;
  753. }
  754. public TextInfo getMessageTextInfo() {
  755. return messageTextInfo;
  756. }
  757. public BottomMenu setMessageTextInfo(TextInfo messageTextInfo) {
  758. this.messageTextInfo = messageTextInfo;
  759. preRefreshUI();
  760. return this;
  761. }
  762. public TextInfo getCancelTextInfo() {
  763. return cancelTextInfo;
  764. }
  765. public BottomMenu setCancelTextInfo(TextInfo cancelTextInfo) {
  766. this.cancelTextInfo = cancelTextInfo;
  767. preRefreshUI();
  768. return this;
  769. }
  770. public int getBackgroundColor() {
  771. return backgroundColor;
  772. }
  773. public BottomMenu setBackgroundColor(@ColorInt int backgroundColor) {
  774. this.backgroundColor = backgroundColor;
  775. preRefreshUI();
  776. return this;
  777. }
  778. public int getSelection() {
  779. return selectionIndex;
  780. }
  781. public ArrayList<Integer> getSelectionList() {
  782. return selectionItems;
  783. }
  784. public BottomMenu setSelection(int selectionIndex) {
  785. this.selectMode = SELECT_MODE.SINGLE;
  786. this.selectionIndex = selectionIndex;
  787. this.selectionItems = null;
  788. menuListAdapter = null;
  789. preRefreshUI();
  790. return this;
  791. }
  792. public BottomMenu setSingleSelection() {
  793. this.selectMode = SELECT_MODE.SINGLE;
  794. this.selectionIndex = -1;
  795. this.selectionItems = null;
  796. menuListAdapter = null;
  797. preRefreshUI();
  798. return this;
  799. }
  800. public BottomMenu setSelection(int[] selectionItems) {
  801. this.selectMode = SELECT_MODE.MULTIPLE;
  802. this.selectionIndex = -1;
  803. this.selectionItems = new ArrayList<>();
  804. if (selectionItems != null) {
  805. for (int itemIndex : selectionItems) {
  806. this.selectionItems.add(itemIndex);
  807. }
  808. }
  809. menuListAdapter = null;
  810. preRefreshUI();
  811. return this;
  812. }
  813. public BottomMenu setMultiSelection() {
  814. this.selectMode = SELECT_MODE.MULTIPLE;
  815. this.selectionIndex = -1;
  816. this.selectionItems = new ArrayList<>();
  817. menuListAdapter = null;
  818. preRefreshUI();
  819. return this;
  820. }
  821. public BottomMenu setSelection(List<Integer> selectionItems) {
  822. this.selectMode = SELECT_MODE.MULTIPLE;
  823. this.selectionIndex = -1;
  824. this.selectionItems = new ArrayList<>(selectionItems);
  825. menuListAdapter = null;
  826. preRefreshUI();
  827. return this;
  828. }
  829. public BottomMenu setNoSelect() {
  830. this.selectMode = SELECT_MODE.NONE;
  831. this.selectionIndex = -1;
  832. this.selectionItems = null;
  833. menuListAdapter = null;
  834. preRefreshUI();
  835. return this;
  836. }
  837. public BottomMenu setBackgroundColorRes(@ColorRes int backgroundRes) {
  838. this.backgroundColor = getColor(backgroundRes);
  839. preRefreshUI();
  840. return this;
  841. }
  842. public CharSequence getOkButton() {
  843. return okText;
  844. }
  845. public BottomMenu setOkButton(CharSequence okText) {
  846. this.okText = okText;
  847. preRefreshUI();
  848. return this;
  849. }
  850. public BottomMenu setOkButton(int OkTextResId) {
  851. this.okText = getString(OkTextResId);
  852. preRefreshUI();
  853. return this;
  854. }
  855. public BottomMenu setOkButton(OnDialogButtonClickListener<BottomDialog> OkButtonClickListener) {
  856. this.okButtonClickListener = OkButtonClickListener;
  857. return this;
  858. }
  859. public BottomMenu setOkButton(CharSequence OkText, OnDialogButtonClickListener<BottomDialog> OkButtonClickListener) {
  860. this.okText = OkText;
  861. this.okButtonClickListener = OkButtonClickListener;
  862. return this;
  863. }
  864. public BottomMenu setOkButton(int OkTextResId, OnDialogButtonClickListener<BottomDialog> OkButtonClickListener) {
  865. this.okText = getString(OkTextResId);
  866. this.okButtonClickListener = OkButtonClickListener;
  867. return this;
  868. }
  869. public CharSequence getOtherButton() {
  870. return otherText;
  871. }
  872. public BottomMenu setOtherButton(CharSequence otherText) {
  873. this.otherText = otherText;
  874. preRefreshUI();
  875. return this;
  876. }
  877. public BottomMenu setOtherButton(int OtherTextResId) {
  878. this.otherText = getString(OtherTextResId);
  879. preRefreshUI();
  880. return this;
  881. }
  882. public BottomMenu setOtherButton(OnDialogButtonClickListener<BottomDialog> OtherButtonClickListener) {
  883. this.otherButtonClickListener = OtherButtonClickListener;
  884. return this;
  885. }
  886. public BottomMenu setOtherButton(CharSequence OtherText, OnDialogButtonClickListener<BottomDialog> OtherButtonClickListener) {
  887. this.otherText = OtherText;
  888. this.otherButtonClickListener = OtherButtonClickListener;
  889. return this;
  890. }
  891. public BottomMenu setOtherButton(int OtherTextResId, OnDialogButtonClickListener<BottomDialog> OtherButtonClickListener) {
  892. this.otherText = getString(OtherTextResId);
  893. this.otherButtonClickListener = OtherButtonClickListener;
  894. return this;
  895. }
  896. public BottomMenu setMaskColor(@ColorInt int maskColor) {
  897. this.maskColor = maskColor;
  898. preRefreshUI();
  899. return this;
  900. }
  901. public long getEnterAnimDuration() {
  902. return enterAnimDuration;
  903. }
  904. public BottomMenu setEnterAnimDuration(long enterAnimDuration) {
  905. this.enterAnimDuration = enterAnimDuration;
  906. return this;
  907. }
  908. public long getExitAnimDuration() {
  909. return exitAnimDuration;
  910. }
  911. public BottomMenu setExitAnimDuration(long exitAnimDuration) {
  912. this.exitAnimDuration = exitAnimDuration;
  913. return this;
  914. }
  915. public SELECT_MODE getSelectMode() {
  916. return selectMode;
  917. }
  918. @Override
  919. protected void shutdown() {
  920. dismiss();
  921. }
  922. }