如何访问 CardView 上的弹出菜单

How to access a popup menu on CardView

我有一个 recyclerview,其中在 recylerview 项目中有一个弹出菜单的图像(见下图),所以我在 cardView 上创建了一个带有编辑和删除选项的弹出菜单我已经尝试了下面的代码但是当我单击弹出菜单按钮,没有任何反应 在我的 NotesAdapter 中,我想检索数据

这是我的 item.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    android:layout_margin="5dp"
    app:cardCornerRadius="10dp"
    app:cardBackgroundColor="@android:color/holo_red_dark"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_marginTop="5dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_marginStart="5dp"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:text="Title"
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"/>

        <ImageView
            android:id="@+id/menuIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="5dp"
            android:layout_marginTop="5dp"
            android:clickable="true"
            app:srcCompat="@drawable/ic_popup_menu" />

    </RelativeLayout>

    <TextView
        android:layout_margin="5dp"
        android:textSize="15sp"
        android:textColor="@android:color/white"
        android:text="Section"
        android:id="@+id/desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

这是我的NotesAdapter.java

public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.MyHolder>
{

    List<Listdata> noteslist;
    private Context context;
    public  NotesAdapter(List<Listdata> noteslist,Context context)
    {
        this.context=context;
        this.noteslist=noteslist;
    }


    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false);

        MyHolder myHolder=new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int position) {
        final Listdata data=noteslist.get(position);
        myHolder.title.setText(data.getTitle());
        myHolder.desc.setText(data.getDesc());



    }

    @Override
    public int getItemCount() {
        return noteslist.size();
    }


    class  MyHolder extends RecyclerView.ViewHolder  {
        TextView title,desc;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            title=itemView.findViewById(R.id.title);
            desc=itemView.findViewById(R.id.desc);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Listdata listdata=noteslist.get(getAdapterPosition());
                    Intent i=new Intent(context, StreamActivity.class);
                    i.putExtra("id",listdata.id);
                    i.putExtra("title",listdata.title);
                    i.putExtra("desc",listdata.desc);
                    context.startActivity(i);
                }
            });

            ImageView menuIcon = itemView.findViewById(R.id.menuIcon);
            menuIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PopupMenu menu = new PopupMenu(v.getContext(), v);
                    menu.setGravity(Gravity.END);
                    menu.getMenu().add("Edit").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {

                            Listdata listdata=noteslist.get(getAdapterPosition());
                            Intent i=new Intent(context, EditActivity.class);
                            i.putExtra("id",listdata.id);
                            i.putExtra("title",listdata.title);
                            i.putExtra("desc",listdata.desc);
                            context.startActivity(i);

                            return false;
                        }
                    });

                    menu.getMenu().add("Delete").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            return false;
                        }
                    });

                }
            });


        }


    }


}

您好,我在您的代码中看到您只是在创建弹出菜单对象但没有显示 按照下面的代码显示弹出菜单。

private void showPopupMenu(View view) {
    // inflate menu
    PopupMenu popup = new PopupMenu(mContext, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_album, popup.getMenu());
    popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
    popup.show();
}

试试这个

@Override
public void onBindViewHolder(@NonNull final MyHolder myHolder, final int position) {
    final Listdata data=noteslist.get(position);
    myHolder.title.setText(data.getTitle());
    myHolder.desc.setText(data.getDesc());
    myHolder.menuIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            PopupMenu popupMenu = new PopupMenu(context, myHolder.menuIcon);
            popupMenu.inflate(R.menu.popup_menu);
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {

                    switch (item.getItemId()) {
                        case R.id.action_edit:
                            Toast.makeText(context, "Edit", Toast.LENGTH_SHORT).show();
                            Intent i=new Intent(context, EditActivity.class);
                            i.putExtra("id",data.id);
                            i.putExtra("title",data.title);
                            i.putExtra("desc",data.desc);
                            context.startActivity(i);
                            break;
                        case R.id.action_delete:
                            noteslist.remove(position);
                            notifyDataSetChanged();
                            Toast.makeText(context, "delete", Toast.LENGTH_SHORT).show();
                            break;
                        default:
                            break;
                    }

                    return false;
                }
            });
            popupMenu.show();
        }
    });



}

@Override
public int getItemCount() {
    return noteslist.size();
}


class  MyHolder extends RecyclerView.ViewHolder  {
    TextView title,desc;
    ImageView menuIcon;

    public MyHolder(@NonNull View itemView) {
        super(itemView);
        menuIcon = itemView.findViewById(R.id.menuIcon);
        title=itemView.findViewById(R.id.title);
        desc=itemView.findViewById(R.id.desc);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Listdata listdata=noteslist.get(getAdapterPosition());
                Intent i=new Intent(context, StreamActivity.class);
                i.putExtra("id",listdata.id);
                i.putExtra("title",listdata.title);
                i.putExtra("desc",listdata.desc);
                context.startActivity(i);
            }
        });


    }

}