我有一个类似谷歌Play的RecyclerView列表和CardView,运行完美。
我需要添加一个弹出式菜单(带溢出图标),就像这样:
哪种方法最好呢?
我研究并发现有两个选项:
1- 在CardView布局中使用工具栏。这种解决方案是否存在性能问题?
2- 使用带有溢出图标的ImageButton或ImageView,点击后创建菜单。
我需要兼容>=API 10的解决方案。
谢谢
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/Not_interasted_catugury"
android:orderInCategory="100"
android:title="Never show stories from this category " />
<item
android:id="@+id/No_interasted"
android:orderInCategory="101"
android:title="Not Interested"></item>
</menu>
<ImageButton
android:id="@+id/imageButton"
android:layout_width="20dp"
android:layout_height="30dp"
android:src="@drawable/ic_dots"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_below="@+id/item_detail"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@null"/>
注意:已保留原文中的HTML标签。然后从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;
}
}
android:background="?android:attr/selectableItemBackground"
或者android:background="?attr/selectableItemBackground"
,这样在点击时会有效果。另外,如果你正在使用矢量图,则应该使用以下代码来获得正确的外观:app:srcCompat="@drawable/ic_more_vert_black_24dp"
和android:tint="@color/secondary_text"
。 - lionscribe这取决于您的布局。
类似于这样的布局
<android.support.v7.widget.CardView>
<LinearLayout>
<Toolbar android:id="@+id/card_toolbar" />
//......
</LinearLayout>
</CardView>
toolbar.inflateMenu(R.menu.card_toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener(){..});
如果您只想使用弹出窗口,那么使用图像会更简单。例如:
PopupMenu popup = new PopupMenu(getContext(), mImageButton);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(......);
popupMenu.inflate(R.menu.menu_restrictions_section);
:) - Alex K.list_item: ( res / layout )
。<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:layout_toStartOf="@+id/item"
android:layout_alignParentStart="true"/>
<ImageButton
android:layout_width="20dp"
android:layout_height="30dp"
android:id="@+id/options"
android:src="@drawable/ic_menu"
android:layout_alignParentEnd="true"
android:onClick="showOptions"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
popup_menu: ( res / menu )
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">>
<item
android:id="@+id/option1"
android:title="Option1"/>
<item
android:id="@+id/option2"
android:title="Option2"/>
</menu>
MainActivity:(在MainActivity
中的任何位置添加此函数)
public void showOptions(View view){
PopupMenu popup = new PopupMenu(MainActivity.this, view);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
CardView view =
(CardView) ((ViewGroup) view.getParent()).getParent();
int position = recyclerView.getChildAdapterPosition(cView);
Toast.makeText(getApplicationContext(),
item.toString()+" clicked at position"+position,
Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
ImageButton
或ImageView
之一即可。 - Joaquim Ley