我想在卡片视图上创建带有编辑和删除选项的弹出式菜单我已经尝试了以下代码,但我的popup.show();
强制关闭onclick
致命异常imageview
。如何在cardview
创建弹出菜单?
public class ShipRecyclerAdapter extends RecyclerView.Adapter<ShipRecyclerAdapter.ViewHolder> {
public static final String PRODUCTID = "PRODUCTID";
public static final String PRODUCTNAME = "PRODUCTNAME";
List<CustDatabaseModel> dbCustList;
static Context context;
public ImageView btndots;
ShipRecyclerAdapter(Context context, List<CustDatabaseModel> dbCustList ){
this.dbCustList = new ArrayList<CustDatabaseModel>();
this.context = context;
this.dbCustList = dbCustList;
}
@Override
public ShipRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.address_list, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(ShipRecyclerAdapter.ViewHolder holder, final int position) {
holder.address.setText(dbCustList.get(position).getaddress());
holder.city.setText(dbCustList.get(position).getcity());
holder.state.setText(dbCustList.get(position).getstate());
holder.zipcode.setText(dbCustList.get(position).getzip());
final String adr=dbCustList.get(position).getaddress().toString();
holder.btndots.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
PopupMenu popup = new PopupMenu(context, btndots);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Intent i;
if ((item.getTitle().toString()).equalsIgnoreCase("Edit")) {
Toast.makeText(ShipRecyclerAdapter.context, "you have clicked Menu image ", Toast.LENGTH_LONG).show();
} else if ((item.getTitle().toString()).equalsIgnoreCase("Delete")) {
Toast.makeText(ShipRecyclerAdapter.context, "you have clicked Menu image ", Toast.LENGTH_LONG).show();
}
return true;
}
});
popup.show();
}
});
//closing the setOnClickListener method
}
@Override
public int getItemCount() {
return dbCustList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView state,address,city,zipcode;
public ImageView btndots;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
address = (TextView) itemLayoutView.findViewById(R.id.textViewAddress);
city = (TextView)itemLayoutView.findViewById(R.id.textViewCity);
zipcode = (TextView)itemLayoutView.findViewById(R.id.textViewZipcode);
state = (TextView)itemLayoutView.findViewById(R.id.textViewState);
this.btndots = (ImageView)itemLayoutView.findViewById(R.id.cmenu);
}
}
}`
答案 0 :(得分:6)
首先在菜单文件夹中创建xml文件,如下所示,
<?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/edit"
android:title="@string/edit"
app:showAsAction="ifRoom" />
<item
android:id="@+id/delete"
android:title="@string/delete"
app:showAsAction="ifRoom" />
</menu>
然后在适配器类中创建方法
private void showPopup(View view, final int position) {
// pass the imageview id
View menuItemView = view.findViewById(R.id.btn_song_list_more);
PopupMenu popup = new PopupMenu(activity, menuItemView);
MenuInflater inflate = popup.getMenuInflater();
inflate.inflate(R.menu.popup_menu, popup.getMenu());
Log.e("position -- " + position);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
// do what you need.
break;
case R.id.delete:
// do what you need .
break;
default:
return false;
}
return false;
}
});
popup.show();
}
调用imageview点击事件的此方法。
答案 1 :(得分:1)
试试这个
在您的活动的Oncreate中,首先使用
注册recyclerView的上下文菜单registerForContextMenu(recyclerView);
并创建上下文菜单,如
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Call");//groupId, itemId, order, title
menu.add(0, v.getId(), 0, "SMS");
}
@Override
public boolean onContextItemSelected(MenuItem item){
if(item.getTitle()=="Call"){
Toast.makeText(getApplicationContext(), "calling code", Toast.LENGTH_LONG).show();
}
else if(item.getTitle()=="SMS"){
Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();
}else{
return false;
}
return true;
}
希望这有助于你