我已经在Activity
和RecyclerViewAdapter
内部创建了一个回收站视图
以下是我用onBindViewHolder()
编写的代码段:
PopupMenu popupMenu = new PopupMenu(activity,holder.ivEventShare);
popupMenu.getMenuInflater().inflate(R.menu.item_event,popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.action_item_event_share:
Toast.makeText(activity, "This Item will be shared", Toast.LENGTH_SHORT)show();
break;
}
return true;
}
});
popupMenu.show();
MenuPopupHelper helper = new MenuPopupHelper(activity, (MenuBuilder) popupMenu.getMenu(),v);
helper.setForceShowIcon(true);
helper.setGravity(Gravity.END);
helper.show();
我创建了
PopUpMenu
,并希望它显示带有图标的菜单。但是它显示以下错误:
com.android.internal.view.menu.MenuBuilder无法转换为android.support.v7.view.menu.MenuBuilder
在这一行:
MenuPopupHelper helper = new MenuPopupHelper(activity, (MenuBuilder) popupMenu.getMenu(),v);
任何帮助,将不胜感激。
最佳答案
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.view.menu.MenuPopupHelper;
import android.support.v7.widget.PopupMenu;
//enter code here
imagePostShare.setOnClickListener(new View.OnClickListener() {
@SuppressLint("RestrictedApi")
@Override
public void onClick(View v) {
popup = new PopupMenu(mContext, v);
popup.getMenuInflater().inflate(R.menu.share_menu, popup.getMenu());
// popup.show();
MenuPopupHelper menuHelper = new MenuPopupHelper(mContext, (MenuBuilder) popup.getMenu(),v);
menuHelper.setForceShowIcon(true);
menuHelper.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.shareAsPost) {
String postId = item.getSharedPostId();
Call<PostShareItem> call = webService.getPostDetails(deviceId,
profileId, token, userIds, postId);
sendShareItemRequest(call);
}
if (id == R.id.shareFacebook) {}
if (id == R.id.shareTwitter) {
Toast.makeText(App.getAppContext(), "Removed : ",
Toast.LENGTH_SHORT).show();
}
if (id == R.id.copyLink) {}
return true;
}
});
}
});