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);
		
		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://stackoverflow.com/questions/3221488/blur-or-dim-background-when-android-popupwindow-active








郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。