本文共 10001 字,大约阅读时间需要 33 分钟。
最近项目里面用到了底部的弹出提示框,UI小姐姐本着设计样式还是ios的好看原则。设计了一个ios样式的底部弹出提示框。OK OK anyway,类似样式并不少见,实现方式有很多,网上随便找一个吧,还不满大街都是。嗯哼,确实不少。但是 !!! 不是讲代码就是讲布局,或者使用方法挺麻烦。
用的时候还要自己手写这部分代码,麻烦不麻烦?作为一名注定要改变世界的程序猿,你让我天天写这个?这是不能忍的。就没有简单的,快捷的,高效的,一句话就能搞定的吗?
有需求就有产品,所以,这个BottomMenu产生了。
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
Step 2. Add the dependency
dependencies { compile 'com.github.zhaolei9527:BottomMenu:v1.0.1' }
Activity文件代码:(一句话,有木有?很简单,有木有?)
基本用法:new BottomMenuFragment(MainActivity.this) .addMenuItems(new MenuItem("从相册选择")) .addMenuItems(new MenuItem("拍照")) .setOnItemClickListener(new BottomMenuFragment.OnItemClickListener() { @Override public void onItemClick(TextView menu_item, int position) { Toast.makeText(MainActivity.this, menu_item.getText().toString().trim(), Toast.LENGTH_SHORT).show(); } }) .show();
带Title用法:
new BottomMenuFragment(MainActivity.this) .setTitle("标题") .addMenuItems(new MenuItem("从相册选择")) .addMenuItems(new MenuItem("拍照")) .setOnItemClickListener(new BottomMenuFragment.OnItemClickListener() { @Override public void onItemClick(TextView menu_item, int position) { Toast.makeText(MainActivity.this, menu_item.getText().toString().trim(), Toast.LENGTH_SHORT).show(); } }) .show();
指定条目样式用法:
new BottomMenuFragment(MainActivity.this) .setTitle("标题") .addMenuItems(new MenuItem("从相册选择", MenuItem.MenuItemStyle.COMMON)) .addMenuItems(new MenuItem("拍照", MenuItem.MenuItemStyle.STRESS)) .setOnItemClickListener(new BottomMenuFragment.OnItemClickListener() { @Override public void onItemClick(TextView menu_item, int position) { Toast.makeText(MainActivity.this, menu_item.getText().toString().trim(), Toast.LENGTH_SHORT).show(); } }) .show();
全部一句话搞定,还有更多功能可以自己发掘一下。
整体结构为三个文件,BottomMenuFragment为弹出主体内容,MenuItem为条目对象的POJO,MenuItemAdapter顾名思义是条目的适配器。代码逻辑实现方法其实挺简单,大同小异,只不过对于代码进行封装使操作更加便捷,简单且迅速。
BottomMenuFragment文件:public class BottomMenuFragment extends DialogFragment { private final String TAG = "BottomMenuFragment"; private Activity context; private OnItemClickListener mOnItemClickListener; private boolean showTitle = false; private String BottomTitle = ""; public BottomMenuFragment(Activity context) { this.context = context; } private List
MenuItemAdapter文件:
public class MenuItemAdapter extends BaseAdapter { private Context context;//运行上下文 private LayoutInflater listContainer; //视图容器 private ListmenuItems; public MenuItemAdapter(Context _context, List _menuItems){ this.context = _context; this.listContainer = LayoutInflater.from(_context); this.menuItems = _menuItems; } @Override public int getCount() { return this.menuItems.size(); } @Override public Object getItem(int position) { if(position >= menuItems.size() || position < 0) { return null; } else { return menuItems.get(position); } } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if(convertView == null) { view = listContainer.inflate(R.layout.menu_item, null); } MenuItem menuItem = menuItems.get(position); TextView textView = (TextView) view.findViewById(R.id.menu_item); textView.setText(menuItem.getText()); if(menuItems.size() == 1) { textView.setBackgroundResource(R.drawable.bottom_menu_btn_selector); } else if(position == 0) { textView.setBackgroundResource(R.drawable.bottom_menu_top_btn_selector); } else if(position < menuItems.size() - 1) { textView.setBackgroundResource(R.drawable.bottom_menu_mid_btn_selector); } else { textView.setBackgroundResource(R.drawable.bottom_menu_bottom_btn_selector); } if(menuItem.getStyle() == MenuItem.MenuItemStyle.COMMON) { textView.setTextColor(ContextCompat.getColor(context, R.color.bottom_menu_btn_text_commom_color)); } else { textView.setTextColor(ContextCompat.getColor(context, R.color.bottom_menu_btn_text_stress_color)); } return view; }}
MenuItem文件:
public class MenuItem { public MenuItem() { } /** * @param _item_name 菜单项名称 * @param _text 菜单项显示内容 * @param _style 菜单类型 */ public MenuItem(String _item_name, String _text, MenuItemStyle _style) { this.item_name = _item_name; this.text = _text; this.style = _style; } public MenuItem(String _text, MenuItemStyle _style) { this.text = _text; this.style = _style; } public MenuItem(String _text) { this.text = _text; } private String item_name; private String text; private MenuItemStyle style = MenuItemStyle.COMMON; public String getItem_name() { return item_name; } public String getText() { return text; } public MenuItemStyle getStyle() { return style; } public void setItem_name(String item_name) { this.item_name = item_name; } public void setText(String text) { this.text = text; } /** * @param style 菜单类型 */ public void setStyle(MenuItemStyle style) { this.style = style; } public enum MenuItemStyle { COMMON, STRESS }}
代码整体满足了一句话搞定高仿ios底部弹出提示框的功能,当然,
有了需求才有了功能,有了想法才有了创作,你的反馈会是使我进步的最大动力。
觉得还不够方便?还想要什么功能?告诉我!欢迎反馈,欢迎Star。源码入口:
转载地址:http://mhgol.baihongyu.com/