大家好,又见面了,我是全栈君。
近期在项目中须要用到下拉菜单。公司比較推崇美团的下拉菜单,于是要实现该功能。想着。这个功能应该是一个常常会用到的。于是何不写一个帮助类,仅仅要往这个类里面传入特定的參数,既能够实现下来菜单,并且还能够实现菜单选择的回调。既能够反复使用,有简单便捷
首先,查看界面效果图
界面倒是比較简单,主要列下功能:
- 这个是靠一个帮助类实现的。下次想在自己的项目中实现该功能,一句引用代码,传入特定的參数既能够实现该功能
- 菜单弹出的时候。背景变灰色。菜单收回,背景回复白色
- 自己主动给选定的选项加入背景色,假设下次选择的其它选项,背景色自己主动切换
- 回调菜单的选择项
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); topView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showMenu(); } }); 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); } }
传送门
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/115247.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...