BottomMenu.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package com.kongzue.dialogx.dialogs;
  2. import android.view.View;
  3. import android.view.ViewGroup;
  4. import android.widget.AdapterView;
  5. import android.widget.BaseAdapter;
  6. import android.widget.RelativeLayout;
  7. import com.kongzue.dialogx.DialogX;
  8. import com.kongzue.dialogx.R;
  9. import com.kongzue.dialogx.interfaces.DialogLifecycleCallback;
  10. import com.kongzue.dialogx.interfaces.DialogXStyle;
  11. import com.kongzue.dialogx.interfaces.OnBackPressedListener;
  12. import com.kongzue.dialogx.interfaces.OnBindView;
  13. import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;
  14. import com.kongzue.dialogx.interfaces.OnIconChangeCallBack;
  15. import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;
  16. import com.kongzue.dialogx.util.NormalMenuArrayAdapter;
  17. import com.kongzue.dialogx.util.views.BottomDialogListView;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import static android.view.View.OVER_SCROLL_NEVER;
  22. /**
  23. * @author: Kongzue
  24. * @github: https://github.com/kongzue/
  25. * @homepage: http://kongzue.com/
  26. * @mail: myzcxhh@live.cn
  27. * @createTime: 2020/10/6 23:48
  28. */
  29. public class BottomMenu extends BottomDialog {
  30. protected BottomMenu me = this;
  31. protected OnMenuItemClickListener<BottomMenu> onMenuItemClickListener;
  32. public static BottomMenu build() {
  33. return new BottomMenu();
  34. }
  35. protected BottomMenu() {
  36. super();
  37. if (style.overrideBottomDialogRes() != null) {
  38. bottomDialogMaxHeight = style.overrideBottomDialogRes().overrideBottomDialogMaxHeight();
  39. }
  40. if (bottomDialogMaxHeight <= 1 && bottomDialogMaxHeight > 0f) {
  41. bottomDialogMaxHeight = (int) (getRootFrameLayout().getMeasuredHeight() * bottomDialogMaxHeight);
  42. }
  43. }
  44. private OnIconChangeCallBack onIconChangeCallBack;
  45. private BottomDialogListView listView;
  46. private BaseAdapter menuListAdapter;
  47. private List<CharSequence> menuList;
  48. public static BottomMenu show(List<CharSequence> menuList) {
  49. BottomMenu bottomMenu = new BottomMenu();
  50. bottomMenu.setMenuList(menuList);
  51. bottomMenu.show();
  52. return bottomMenu;
  53. }
  54. public static BottomMenu showStringList(List<String> menuList) {
  55. BottomMenu bottomMenu = new BottomMenu();
  56. bottomMenu.setMenuStringList(menuList);
  57. bottomMenu.show();
  58. return bottomMenu;
  59. }
  60. public static BottomMenu show(String[] menuList) {
  61. BottomMenu bottomMenu = new BottomMenu();
  62. bottomMenu.setMenuList(menuList);
  63. bottomMenu.show();
  64. return bottomMenu;
  65. }
  66. public static BottomMenu show(CharSequence[] menuList) {
  67. BottomMenu bottomMenu = new BottomMenu();
  68. bottomMenu.setMenuList(menuList);
  69. bottomMenu.show();
  70. return bottomMenu;
  71. }
  72. @Override
  73. protected void onDialogInit(final DialogImpl dialog) {
  74. if (dialog != null) {
  75. if (!isAllowInterceptTouch()) {
  76. dialog.bkg.setMaxHeight((int) bottomDialogMaxHeight);
  77. if (bottomDialogMaxHeight != 0) {
  78. dialogImpl.scrollView.setEnabled(false);
  79. }
  80. }
  81. int dividerDrawableResId = isLightTheme() ? R.drawable.rect_dialogx_material_menu_split_divider : R.drawable.rect_dialogx_material_menu_split_divider_night;
  82. int dividerHeight = 1;
  83. if (style.overrideBottomDialogRes() != null) {
  84. dividerDrawableResId = style.overrideBottomDialogRes().overrideMenuDividerDrawableRes(isLightTheme());
  85. dividerHeight = style.overrideBottomDialogRes().overrideMenuDividerHeight(isLightTheme());
  86. }
  87. listView = new BottomDialogListView(getContext());
  88. listView.setOverScrollMode(OVER_SCROLL_NEVER);
  89. listView.setDivider(getResources().getDrawable(dividerDrawableResId));
  90. listView.setDividerHeight(dividerHeight);
  91. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  92. @Override
  93. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  94. if (onMenuItemClickListener != null) {
  95. if (!onMenuItemClickListener.onClick(me, menuList.get(position), position)) {
  96. dismiss();
  97. }
  98. } else {
  99. dismiss();
  100. }
  101. }
  102. });
  103. if (style.overrideBottomDialogRes() != null) {
  104. if (style.overrideBottomDialogRes().overrideMenuItemLayout(true, 0, 1) != 0) {
  105. listView.setSelector(R.color.empty);
  106. }
  107. }
  108. RelativeLayout.LayoutParams listViewLp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  109. dialog.boxCustom.addView(listView, listViewLp);
  110. refreshUI();
  111. }
  112. }
  113. @Override
  114. public void refreshUI() {
  115. super.refreshUI();
  116. if (listView != null) {
  117. if (menuListAdapter == null) {
  118. menuListAdapter = new NormalMenuArrayAdapter(me, getContext(), menuList);
  119. }
  120. if (listView.getAdapter() == null) {
  121. listView.setAdapter(menuListAdapter);
  122. } else {
  123. if (listView.getAdapter() != menuListAdapter) {
  124. listView.setAdapter(menuListAdapter);
  125. } else {
  126. menuListAdapter.notifyDataSetChanged();
  127. }
  128. }
  129. }
  130. }
  131. public List<CharSequence> getMenuList() {
  132. return menuList;
  133. }
  134. public BottomMenu setMenuList(List<CharSequence> menuList) {
  135. this.menuList = menuList;
  136. refreshUI();
  137. return this;
  138. }
  139. public BottomMenu setMenuStringList(List<String> menuList) {
  140. this.menuList = new ArrayList<>();
  141. this.menuList.addAll(menuList);
  142. refreshUI();
  143. return this;
  144. }
  145. public BottomMenu setMenuList(String[] menuList) {
  146. this.menuList = new ArrayList<>();
  147. this.menuList.addAll(Arrays.asList(menuList));
  148. refreshUI();
  149. return this;
  150. }
  151. public BottomMenu setMenuList(CharSequence[] menuList) {
  152. this.menuList = Arrays.asList(menuList);
  153. refreshUI();
  154. return this;
  155. }
  156. public OnIconChangeCallBack getOnIconChangeCallBack() {
  157. return onIconChangeCallBack;
  158. }
  159. public BottomMenu setOnIconChangeCallBack(OnIconChangeCallBack onIconChangeCallBack) {
  160. this.onIconChangeCallBack = onIconChangeCallBack;
  161. return this;
  162. }
  163. public OnBackPressedListener getOnBackPressedListener() {
  164. return onBackPressedListener;
  165. }
  166. public BottomMenu setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
  167. this.onBackPressedListener = onBackPressedListener;
  168. refreshUI();
  169. return this;
  170. }
  171. public BottomMenu setDialogLifecycleCallback(DialogLifecycleCallback<BottomDialog> dialogLifecycleCallback) {
  172. this.dialogLifecycleCallback = dialogLifecycleCallback;
  173. return this;
  174. }
  175. public BottomMenu setStyle(DialogXStyle style) {
  176. this.style = style;
  177. return this;
  178. }
  179. public BottomMenu setTheme(DialogX.THEME theme) {
  180. this.theme = theme;
  181. return this;
  182. }
  183. public boolean isCancelable() {
  184. return cancelable;
  185. }
  186. public BottomMenu setCancelable(boolean cancelable) {
  187. this.cancelable = cancelable;
  188. refreshUI();
  189. return this;
  190. }
  191. public DialogImpl getDialogImpl() {
  192. return dialogImpl;
  193. }
  194. public CharSequence getTitle() {
  195. return title;
  196. }
  197. public BottomMenu setTitle(CharSequence title) {
  198. this.title = title;
  199. refreshUI();
  200. return this;
  201. }
  202. public CharSequence getMessage() {
  203. return message;
  204. }
  205. public BottomMenu setMessage(CharSequence message) {
  206. this.message = message;
  207. refreshUI();
  208. return this;
  209. }
  210. public CharSequence getCancelButton() {
  211. return cancelText;
  212. }
  213. public BottomMenu setCancelButton(CharSequence cancelText) {
  214. this.cancelText = cancelText;
  215. refreshUI();
  216. return this;
  217. }
  218. public BottomMenu setCancelButton(OnDialogButtonClickListener cancelButtonClickListener) {
  219. this.cancelButtonClickListener = cancelButtonClickListener;
  220. return this;
  221. }
  222. public BottomMenu setCancelButton(CharSequence cancelText,OnDialogButtonClickListener cancelButtonClickListener) {
  223. this.cancelText = cancelText;
  224. this.cancelButtonClickListener = cancelButtonClickListener;
  225. refreshUI();
  226. return this;
  227. }
  228. public BottomMenu setCustomView(OnBindView<BottomDialog> onBindView) {
  229. this.onBindView = onBindView;
  230. refreshUI();
  231. return this;
  232. }
  233. public View getCustomView() {
  234. if (onBindView == null) return null;
  235. return onBindView.getCustomView();
  236. }
  237. public BottomMenu removeCustomView() {
  238. this.onBindView.clean();
  239. refreshUI();
  240. return this;
  241. }
  242. public boolean isAllowInterceptTouch() {
  243. return super.isAllowInterceptTouch();
  244. }
  245. public BottomMenu setAllowInterceptTouch(boolean allowInterceptTouch) {
  246. this.allowInterceptTouch = allowInterceptTouch;
  247. refreshUI();
  248. return this;
  249. }
  250. public BottomMenu setDialogImpl(DialogImpl dialogImpl) {
  251. this.dialogImpl = dialogImpl;
  252. return this;
  253. }
  254. public float getBottomDialogMaxHeight() {
  255. return bottomDialogMaxHeight;
  256. }
  257. public BottomMenu setBottomDialogMaxHeight(float bottomDialogMaxHeight) {
  258. this.bottomDialogMaxHeight = bottomDialogMaxHeight;
  259. return this;
  260. }
  261. public OnMenuItemClickListener<BottomMenu> getOnMenuItemClickListener() {
  262. return onMenuItemClickListener;
  263. }
  264. public BottomMenu setOnMenuItemClickListener(OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
  265. this.onMenuItemClickListener = onMenuItemClickListener;
  266. return this;
  267. }
  268. public BaseAdapter getMenuListAdapter() {
  269. return menuListAdapter;
  270. }
  271. public BottomMenu setMenuListAdapter(BaseAdapter menuListAdapter) {
  272. this.menuListAdapter = menuListAdapter;
  273. return this;
  274. }
  275. public OnDialogButtonClickListener getCancelButtonClickListener() {
  276. return cancelButtonClickListener;
  277. }
  278. public BottomMenu setCancelButtonClickListener(OnDialogButtonClickListener cancelButtonClickListener) {
  279. this.cancelButtonClickListener = cancelButtonClickListener;
  280. return this;
  281. }
  282. }