我有一个列表,看起来像带有cardview的recyclerview中的谷歌播放,并且工作完美.
我需要添加一个弹出菜单(带溢出图标),如下所示:
这是最好的方法吗?
我研究发现有两种选择:
1 - 在cardview布局中有一个工具栏.这个解决方案有一个性能问题吗?
2 - 带有带溢出图标的图像按钮或图像视图,即单击菜单时创建.
我需要一个与> = API 10兼容的解决方案
谢谢
然后从drawable中给出一个图标
并在onBindViewHolder中设置项目单击列表器
mImageButton= (ImageButton) view.findViewById(R.id.imageButton); holder.mImageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showPopupMenu(holder.mImageButton,position); } });
private void showPopupMenu(View view,int position) { // inflate menu PopupMenu popup = new PopupMenu(view.getContext(),view ); MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.popup_menu, popup.getMenu()); popup.setOnMenuItemClickListener(new MyMenuItemClickListener(position)); popup.show(); }
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener { private int position; public MyMenuItemClickListener(int positon) { this.position=positon; } @Override public boolean onMenuItemClick(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.Not_interasted_catugury: String RemoveCategory=mDataSet.get(position).getCategory(); // mDataSet.remove(position); //notifyItemRemoved(position); // notifyItemRangeChanged(position,mDataSet.size()); mySharedPreferences.saveStringPrefs(Constants.REMOVE_CTAGURY,RemoveCategory,MainActivity.context); Toast.makeText(MainActivity.context, "Add to favourite", Toast.LENGTH_SHORT).show(); return true; case R.id.No_interasted: mDataSet.remove(position); notifyItemRemoved(position); notifyItemRangeChanged(position,mDataSet.size()); Toast.makeText(MainActivity.context, "Done for now", Toast.LENGTH_SHORT).show(); return true; case R.id.delete: mySharedPreferences.deletePrefs(Constants.REMOVE_CTAGURY,MainActivity.context); default: } return false; } }
这取决于你的布局.
如果您想要这样的布局,使用工具栏可以更轻松地实现它.
Somenthing喜欢
toolbar.inflateMenu(R.menu.card_toolbar); toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener(){..}); //......
如果您只想使用弹出窗口更简单地使用图像.Somenthing喜欢:
PopupMenu popup = new PopupMenu(getContext(), mImageButton); MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(......);