Android开发:仿美团下拉列表菜单,帮助类,复用简单

时间:2021-01-24 15:56:31

近期在项目中须要用到下拉菜单。公司比較推崇美团的下拉菜单,于是要实现该功能。想着。这个功能应该是一个常常会用到的。于是何不写一个帮助类,仅仅要往这个类里面传入特定的參数,既能够实现下来菜单,并且还能够实现菜单选择的回调。既能够反复使用,有简单便捷

首先,查看界面效果图

Android开发:仿美团下拉列表菜单,帮助类,复用简单

Android开发:仿美团下拉列表菜单,帮助类,复用简单

界面倒是比較简单,主要列下功能:

  1. 这个是靠一个帮助类实现的。下次想在自己的项目中实现该功能,一句引用代码,传入特定的參数既能够实现该功能
  2. 菜单弹出的时候。背景变灰色。菜单收回,背景回复白色
  3. 自己主动给选定的选项加入背景色,假设下次选择的其它选项,背景色自己主动切换
  4. 回调菜单的选择项
假设你须要的是一级选择菜单,如上的功能是全然足够了,好了,一下是代码部分:

下拉菜单的帮助类:MenuHelper
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);
}
}
}
}


代码都比較简单,构造函数须要传的參数中的containerView是一个须要变灰色效果的Layout。你能够传或者设置null

以下是回调接口
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);
} }


好了。自从代码介绍,下拉菜单功能就此实现,下次碰到有下拉菜单的时候,直接五分钟就集成好了

当然,少不了源代码,传送门


參考文档:
Android开发之多级下拉列表菜单实现(仿美团,淘宝等)http://blog.csdn.net/minimicall/article/details/39484493
Blur or dim background when Android PopupWindow active   http://*.com/questions/3221488/blur-or-dim-background-when-android-popupwindow-active