Android开发:仿美团下拉列表菜单,帮助类,复用简单
最近在项目中需要用到下拉菜单,公司比较推崇美团的下拉菜单,于是要实现该功能,想着,这个功能应该是一个经常会用到的,于是何不写一个帮助类,只要往这个类里面传入特定的参数,既可以实现下来菜单,而且还可以实现菜单选择的回调,既可以重复使用,有简单便捷
首先,查看界面效果图
界面倒是比较简单,主要列下功能:
- 这个是靠一个帮助类实现的,下次想在自己的项目中实现该功能,一句引用代码,传入特定的参数既可以实现该功能
- 菜单弹出的时候,背景变灰色,菜单收回,背景回复白色
- 自动给选定的选项添加背景色,如果下次选择的其他选项,背景色自动切换
- 回调菜单的选择项
public class MenuHelper { private PopupWindow popupWindow; private ListView listView; private List<String> data; private Context mContext; private View topView; private ListAdapter adapter; private int i = 0; private FrameLayout container; public MenuHelper(Context context, View topView, final OnMenuClick clickListener, List<String> data, FrameLayout containerView) { mContext = context; this.topView = topView; this.data = data; this.container = containerView; container.getForeground().setAlpha(0); initListView(clickListener); initPopupWindow(); } private void initListView(final OnMenuClick clickListener) { listView = new ListView(mContext); listView.setDivider(null); listView.setDividerHeight(0); listView.setBackgroundColor(Color.WHITE); adapter = new ListAdapter(mContext); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { i = position; clickListener.onPopupMenuClick(position); popupWindow.dismiss(); } }); } private void initPopupWindow() { popupWindow = new PopupWindow(listView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); popupWindow.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() { if (container != null) { container.getForeground().setAlpha(0); } } }); } public void showMenu() { adapter.notifyDataSetChanged(); if (popupWindow.isShowing()) { popupWindow.dismiss(); }else { popupWindow.setOutsideTouchable(true); popupWindow.setTouchable(true); popupWindow.showAsDropDown(topView, 0, 0); if (container != null) { container.getForeground().setAlpha(120); } } } private class ListAdapter extends ArrayAdapter<String> { public ListAdapter(Context context) { super(context, R.layout.item_text, data); } private Holder getHolder(final View view) { Holder holder = (Holder) view.getTag(); if (holder == null) { holder = new Holder(view); view.setTag(holder); } return holder; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View rowView = convertView; if (rowView == null) { LayoutInflater inflater = LayoutInflater.from(mContext); rowView = inflater.inflate(R.layout.item_text, null); } final Holder holder = getHolder(rowView); holder.textview.setText(data.get(position)); if (position == i) { holder.textview.setBackgroundColor(mContext.getResources().getColor(R.color.item_press)); }else { holder.textview.setBackgroundColor(Color.TRANSPARENT); } return rowView; } private class Holder { public TextView textview; public Holder(View view) { textview = (TextView) view.findViewById(R.id.textView); } } } }
public interface OnMenuClick { public void onPopupMenuClick(int position); }
最后,是MainActivity的代码:
public class MainActivity extends Activity implements OnMenuClick{ private MenuHelper mMenuHelper; private Button button; private FrameLayout container; private List<String> menuData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); container = (FrameLayout) findViewById(R.id.container); button = (Button) findViewById(R.id.button); menuData = new ArrayList<String>(); menuData.add("ladfj"); menuData.add("ladfj"); menuData.add("ladfj"); menuData.add("ladfj"); menuData.add("ladfj"); mMenuHelper = new MenuHelper(this, button, this, menuData, container); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mMenuHelper.showMenu(); } }); } @Override public void onPopupMenuClick(int position) { Log.d("debug", "click position " + position); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。