我正在开发有RecyclerView
的应用。我需要调用ContextMenu
,其中指向删除RecyclerView
中的当前项目。
class FavoriteQuotesHolder extends RecyclerView.ViewHolder
implements View.OnLongClickListener, View.OnCreateContextMenuListener {
CardView cardView;
TextView contentOfQuote, authorQuote;
LinearLayout linearLayout;
FavoriteQuotesHolder(View itemView) {
super(itemView);
itemView.setOnCreateContextMenuListener(this);
this.cardView = (CardView) itemView.findViewById(R.id.cardView);
this.contentOfQuote = (TextView) itemView.findViewById(R.id.contentOfQuote);
this.authorQuote = (TextView) itemView.findViewById(R.id.authorQuote);
this.linearLayout = (LinearLayout) itemView.findViewById(R.id.main);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, 0, Menu.NONE, "Edit");
menu.add(Menu.NONE, 1, Menu.NONE, "Delete");
}
@Override
public boolean onLongClick(View v) {
return false;
}
}
我不明白我将如何引用当前项目菜单中的项目
@Override
public void onBindViewHolder(FavoriteQuotesHolder holder, int position) {
LikedQuote likedQuote = getItem(position);
holder.contentOfQuote.setText(likedQuote.getQuote());
holder.authorQuote.setText(likedQuote.getAuthor());
holder.itemView.????????????
}
答案 0 :(得分:0)
活动需要注册回收者视图长按:
registerForContextMenu(recyclerView);
项目视图应该是长按:
viewHolder.itemView.setLongClickable(true);
在itemView上实现setOnlongClickListner:
itemView.setOnLongClickListener(new OnLongClickListener);
答案 1 :(得分:0)
这就是想要的:https://stackoverflow.com/a/39602856/104085
最好的方法是将此功能绑定到自定义Viewholder类。