Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

java - Displaying Content Menu When Image on RecyclerView Item Clicked

I have design my RecyclerView to display something like this:

My RecyclerView

I want to show context menu when user clicked and ImageView (the vertical 3 dots image) in RecyclerView item.

@Override
public void onBindViewHolder(final VendorViewHolder holder, final int position) {
    holder.nameText.setText(this.vendors.get(position).getName());
    holder.contextMenuBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Code to show ContextMenu here
        }
    });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Put that 3 dot image and on click of that image use the following,

//creating a popup menu
PopupMenu popup = new PopupMenu(context, holder.addresslistmenu);
//inflating menu from xml resource
popup.inflate(R.menu.addressmenu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.addresslistupdatemenu:
                                yourfunction()
                                break;
                        }
                        return false;
                    }
                });
//displaying the popup
popup.show();

Add the menu item in res-menu,

example ::

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/addresslistupdatemenu"
        android:title="Update" />

    <item
        android:id="@+id/addresslistdeletemenu"
        android:title="Delete" />
</menu>

Image click is in onBindViewHolder in the adapter.

holder.image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                     // PopupMenu function here
                }
  });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...