我有一个完美的自定义列表视图.现在,我希望列表中的每个项目都有一个下拉菜单,这样当用户点击列表中的任何项目时,下拉菜单会显示两个选项 - 编辑和删除.根据用户的选择,列表视图项被处理.
我想知道将下拉菜单添加到每个listview项的方法.
列表项的xml文件 -
适配器类
public class list_addr_adapter extends ArrayAdapter{ Context context; int layoutResourceId; public list_addr_adapter(Context context, int layoutResourceId, List items) { super(context, layoutResourceId, items); this.layoutResourceId = layoutResourceId; this.context = context; // this.listener=callback; } /*private view holder class*/ private class ViewHolder { TextView txtTitle; TextView txtDetail; } ViewHolder holder = null; public View getView(int position, View convertView, ViewGroup parent) { final list_addr lists = getItem(position); final int pos=position; LayoutInflater mInflater = (LayoutInflater) context .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = mInflater.inflate(R.layout.items_sav_addr2, null); holder = new ViewHolder(); holder.txtTitle = (TextView) convertView.findViewById(R.id.textTitle); holder.txtDetail = (TextView) convertView.findViewById(R.id.detail); convertView.setTag(holder); } else holder = (ViewHolder) convertView.getTag(); holder.txtTitle.setText(lists.getTitle()); holder.txtDetail.setText(lists.getDetail()); return convertView; } }
我希望它是这样的 -
检查一下:
XML:
// Replace Image
适配器类:
public class list_addr_adapter extends ArrayAdapter{ Context context; int layoutResourceId; public list_addr_adapter(Context context, int layoutResourceId, List items) { super(context, layoutResourceId, items); this.layoutResourceId = layoutResourceId; this.context = context; // this.listener=callback; } /*private view holder class*/ private class ViewHolder { TextView txtTitle; TextView txtDetail; ImageView imageview; } ViewHolder holder = null; public View getView(int position, View convertView, ViewGroup parent) { final list_addr lists = getItem(position); final int pos=position; LayoutInflater mInflater = (LayoutInflater) context .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = mInflater.inflate(R.layout.items_sav_addr2, null); holder = new ViewHolder(); holder.txtTitle = (TextView) convertView.findViewById(R.id.textTitle); holder.txtDetail = (TextView) convertView.findViewById(R.id.detail); holder.imageview = (ImageView) convertView.findViewById(R.id.imageview); convertView.setTag(holder); } else holder = (ViewHolder) convertView.getTag(); holder.txtTitle.setText(lists.getTitle()); holder.txtDetail.setText(lists.getDetail()); try { holder.imageview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.imageview: PopupMenu popup = new PopupMenu(getApplicationContext(), v); popup.getMenuInflater().inflate(R.menu.clipboard_popup, popup.getMenu()); popup.show(); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.edit: //Or Some other code you want to put here.. This is just an example. Toast.makeText(getApplicationContext(), " Install Clicked at position " + " : " + position, Toast.LENGTH_LONG).show(); break; case R.id.delete: Toast.makeText(getApplicationContext(), "Add to Wish List Clicked at position " + " : " + position, Toast.LENGTH_LONG).show(); break; default: break; } return true; } }); break; default: break; } } }); } catch (Exception e) { e.printStackTrace(); } return convertView; } }
弹出menu.xml
请参阅:Android列表视图布局与Google Play类似