使用上下文菜单

时间:2017-06-07 13:09:13

标签: android android-recyclerview

我正在开发有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.????????????
}

2 个答案:

答案 0 :(得分:0)

活动需要注册回收者视图长按:

registerForContextMenu(recyclerView);

项目视图应该是长按:

viewHolder.itemView.setLongClickable(true);

在itemView上实现setOnlongClickListner:

itemView.setOnLongClickListener(new OnLongClickListener);

答案 1 :(得分:0)

这就是想要的:https://stackoverflow.com/a/39602856/104085

最好的方法是将此功能绑定到自定义Viewholder类。