123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- package com.kongzue.dialogx.dialogs;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.RelativeLayout;
- import com.kongzue.dialogx.DialogX;
- import com.kongzue.dialogx.R;
- import com.kongzue.dialogx.interfaces.DialogLifecycleCallback;
- import com.kongzue.dialogx.interfaces.DialogXStyle;
- import com.kongzue.dialogx.interfaces.OnBackPressedListener;
- import com.kongzue.dialogx.interfaces.OnBindView;
- import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;
- import com.kongzue.dialogx.interfaces.OnIconChangeCallBack;
- import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;
- import com.kongzue.dialogx.util.NormalMenuArrayAdapter;
- import com.kongzue.dialogx.util.views.BottomDialogListView;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import static android.view.View.OVER_SCROLL_NEVER;
- /**
- * @author: Kongzue
- * @github: https://github.com/kongzue/
- * @homepage: http://kongzue.com/
- * @mail: myzcxhh@live.cn
- * @createTime: 2020/10/6 23:48
- */
- public class BottomMenu extends BottomDialog {
-
- protected BottomMenu me = this;
-
- protected OnMenuItemClickListener<BottomMenu> onMenuItemClickListener;
-
- public static BottomMenu build() {
- return new BottomMenu();
- }
-
- protected BottomMenu() {
- super();
- if (style.overrideBottomDialogRes() != null) {
- bottomDialogMaxHeight = style.overrideBottomDialogRes().overrideBottomDialogMaxHeight();
- }
- if (bottomDialogMaxHeight <= 1 && bottomDialogMaxHeight > 0f) {
- bottomDialogMaxHeight = (int) (getRootFrameLayout().getMeasuredHeight() * bottomDialogMaxHeight);
- }
- }
-
- private OnIconChangeCallBack onIconChangeCallBack;
- private BottomDialogListView listView;
- private BaseAdapter menuListAdapter;
- private List<CharSequence> menuList;
-
- public static BottomMenu show(List<CharSequence> menuList) {
- BottomMenu bottomMenu = new BottomMenu();
- bottomMenu.setMenuList(menuList);
- bottomMenu.show();
- return bottomMenu;
- }
-
- public static BottomMenu showStringList(List<String> menuList) {
- BottomMenu bottomMenu = new BottomMenu();
- bottomMenu.setMenuStringList(menuList);
- bottomMenu.show();
- return bottomMenu;
- }
-
- public static BottomMenu show(String[] menuList) {
- BottomMenu bottomMenu = new BottomMenu();
- bottomMenu.setMenuList(menuList);
- bottomMenu.show();
- return bottomMenu;
- }
-
- public static BottomMenu show(CharSequence[] menuList) {
- BottomMenu bottomMenu = new BottomMenu();
- bottomMenu.setMenuList(menuList);
- bottomMenu.show();
- return bottomMenu;
- }
-
- @Override
- protected void onDialogInit(final DialogImpl dialog) {
- if (dialog != null) {
-
- if (!isAllowInterceptTouch()) {
- dialog.bkg.setMaxHeight((int) bottomDialogMaxHeight);
- if (bottomDialogMaxHeight != 0) {
- dialogImpl.scrollView.setEnabled(false);
- }
- }
-
- int dividerDrawableResId = isLightTheme() ? R.drawable.rect_dialogx_material_menu_split_divider : R.drawable.rect_dialogx_material_menu_split_divider_night;
- int dividerHeight = 1;
- if (style.overrideBottomDialogRes() != null) {
- dividerDrawableResId = style.overrideBottomDialogRes().overrideMenuDividerDrawableRes(isLightTheme());
- dividerHeight = style.overrideBottomDialogRes().overrideMenuDividerHeight(isLightTheme());
- }
-
- listView = new BottomDialogListView(getContext());
- listView.setOverScrollMode(OVER_SCROLL_NEVER);
- listView.setDivider(getResources().getDrawable(dividerDrawableResId));
- listView.setDividerHeight(dividerHeight);
-
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- if (onMenuItemClickListener != null) {
- if (!onMenuItemClickListener.onClick(me, menuList.get(position), position)) {
- dismiss();
- }
- } else {
- dismiss();
- }
- }
- });
- if (style.overrideBottomDialogRes() != null) {
- if (style.overrideBottomDialogRes().overrideMenuItemLayout(true, 0, 1) != 0) {
- listView.setSelector(R.color.empty);
- }
- }
-
- RelativeLayout.LayoutParams listViewLp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- dialog.boxCustom.addView(listView, listViewLp);
-
- refreshUI();
- }
- }
-
- @Override
- public void refreshUI() {
- super.refreshUI();
- if (listView != null) {
- if (menuListAdapter == null) {
- menuListAdapter = new NormalMenuArrayAdapter(me, getContext(), menuList);
- }
- if (listView.getAdapter() == null) {
- listView.setAdapter(menuListAdapter);
- } else {
- if (listView.getAdapter() != menuListAdapter) {
- listView.setAdapter(menuListAdapter);
- } else {
- menuListAdapter.notifyDataSetChanged();
- }
- }
- }
- }
-
- public List<CharSequence> getMenuList() {
- return menuList;
- }
-
- public BottomMenu setMenuList(List<CharSequence> menuList) {
- this.menuList = menuList;
- refreshUI();
- return this;
- }
-
- public BottomMenu setMenuStringList(List<String> menuList) {
- this.menuList = new ArrayList<>();
- this.menuList.addAll(menuList);
- refreshUI();
- return this;
- }
-
- public BottomMenu setMenuList(String[] menuList) {
- this.menuList = new ArrayList<>();
- this.menuList.addAll(Arrays.asList(menuList));
- refreshUI();
- return this;
- }
-
- public BottomMenu setMenuList(CharSequence[] menuList) {
- this.menuList = Arrays.asList(menuList);
- refreshUI();
- return this;
- }
-
- public OnIconChangeCallBack getOnIconChangeCallBack() {
- return onIconChangeCallBack;
- }
-
- public BottomMenu setOnIconChangeCallBack(OnIconChangeCallBack onIconChangeCallBack) {
- this.onIconChangeCallBack = onIconChangeCallBack;
- return this;
- }
-
- public OnBackPressedListener getOnBackPressedListener() {
- return onBackPressedListener;
- }
-
- public BottomMenu setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
- this.onBackPressedListener = onBackPressedListener;
- refreshUI();
- return this;
- }
-
- public BottomMenu setDialogLifecycleCallback(DialogLifecycleCallback<BottomDialog> dialogLifecycleCallback) {
- this.dialogLifecycleCallback = dialogLifecycleCallback;
- return this;
- }
-
- public BottomMenu setStyle(DialogXStyle style) {
- this.style = style;
- return this;
- }
-
- public BottomMenu setTheme(DialogX.THEME theme) {
- this.theme = theme;
- return this;
- }
-
- public boolean isCancelable() {
- return cancelable;
- }
-
- public BottomMenu setCancelable(boolean cancelable) {
- this.cancelable = cancelable;
- refreshUI();
- return this;
- }
-
- public DialogImpl getDialogImpl() {
- return dialogImpl;
- }
-
- public CharSequence getTitle() {
- return title;
- }
-
- public BottomMenu setTitle(CharSequence title) {
- this.title = title;
- refreshUI();
- return this;
- }
-
- public CharSequence getMessage() {
- return message;
- }
-
- public BottomMenu setMessage(CharSequence message) {
- this.message = message;
- refreshUI();
- return this;
- }
-
- public CharSequence getCancelButton() {
- return cancelText;
- }
-
- public BottomMenu setCancelButton(CharSequence cancelText) {
- this.cancelText = cancelText;
- refreshUI();
- return this;
- }
-
- public BottomMenu setCancelButton(OnDialogButtonClickListener cancelButtonClickListener) {
- this.cancelButtonClickListener = cancelButtonClickListener;
- return this;
- }
-
- public BottomMenu setCancelButton(CharSequence cancelText,OnDialogButtonClickListener cancelButtonClickListener) {
- this.cancelText = cancelText;
- this.cancelButtonClickListener = cancelButtonClickListener;
- refreshUI();
- return this;
- }
-
- public BottomMenu setCustomView(OnBindView<BottomDialog> onBindView) {
- this.onBindView = onBindView;
- refreshUI();
- return this;
- }
-
- public View getCustomView() {
- if (onBindView == null) return null;
- return onBindView.getCustomView();
- }
-
- public BottomMenu removeCustomView() {
- this.onBindView.clean();
- refreshUI();
- return this;
- }
-
- public boolean isAllowInterceptTouch() {
- return super.isAllowInterceptTouch();
- }
-
- public BottomMenu setAllowInterceptTouch(boolean allowInterceptTouch) {
- this.allowInterceptTouch = allowInterceptTouch;
- refreshUI();
- return this;
- }
-
- public BottomMenu setDialogImpl(DialogImpl dialogImpl) {
- this.dialogImpl = dialogImpl;
- return this;
- }
-
- public float getBottomDialogMaxHeight() {
- return bottomDialogMaxHeight;
- }
-
- public BottomMenu setBottomDialogMaxHeight(float bottomDialogMaxHeight) {
- this.bottomDialogMaxHeight = bottomDialogMaxHeight;
- return this;
- }
-
- public OnMenuItemClickListener<BottomMenu> getOnMenuItemClickListener() {
- return onMenuItemClickListener;
- }
-
- public BottomMenu setOnMenuItemClickListener(OnMenuItemClickListener<BottomMenu> onMenuItemClickListener) {
- this.onMenuItemClickListener = onMenuItemClickListener;
- return this;
- }
-
- public BaseAdapter getMenuListAdapter() {
- return menuListAdapter;
- }
-
- public BottomMenu setMenuListAdapter(BaseAdapter menuListAdapter) {
- this.menuListAdapter = menuListAdapter;
- return this;
- }
-
- public OnDialogButtonClickListener getCancelButtonClickListener() {
- return cancelButtonClickListener;
- }
-
- public BottomMenu setCancelButtonClickListener(OnDialogButtonClickListener cancelButtonClickListener) {
- this.cancelButtonClickListener = cancelButtonClickListener;
- return this;
- }
- }
|