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
502 views
in Technique[技术] by (71.8m points)

android - Toolbar inside CardView to create a popup menu (overflow icon)

I have a list that looks like google play in a recyclerview with cardview, and works perfect.

I need to add a popup menu (with overflow icon), like this:

which is the best way to do this ?

I researched and found that there are 2 options:

1 - with a toolbar inside the cardview layout. is there a performanece problem with this solution ?

2 - with a imagebutton or imageview with a icon of the overflow, that when you click the menu is created.

I need a solution to be compatible with >= API 10

thanks

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Step 1 create Popup Menu xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/Not_interasted_catugury"
        android:orderInCategory="100"
        android:title="Never show stories from this category  " />

    <item
        android:id="@+id/No_interasted"
        android:orderInCategory="101"
        android:title="Not Interested"></item>


</menu>

Step 2: Make a Image button in your card

 <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="20dp"
            android:layout_height="30dp"
            android:src="@drawable/ic_dots"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:layout_below="@+id/item_detail"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:background="@null"/>

then give a icon from drawable

Step 3: Inside your holder class

and set item click listner inside onBindViewHolder

mImageButton= (ImageButton) view.findViewById(R.id.imageButton);
holder.mImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupMenu(holder.mImageButton,position);
            }
        });

Step 4: Show pop menu and inflate the xml

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

Step 5: Implement the OnMenuItemClickListener

     class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {

        private int position;
        public MyMenuItemClickListener(int positon) {
            this.position=positon;
        }

        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {

                case R.id.Not_interasted_catugury:
                    String RemoveCategory=mDataSet.get(position).getCategory();
                    // mDataSet.remove(position);
                    //notifyItemRemoved(position);
                   // notifyItemRangeChanged(position,mDataSet.size());

                    mySharedPreferences.saveStringPrefs(Constants.REMOVE_CTAGURY,RemoveCategory,MainActivity.context);
                    Toast.makeText(MainActivity.context, "Add to favourite", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.No_interasted:
                    mDataSet.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position,mDataSet.size());
                    Toast.makeText(MainActivity.context, "Done for now", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.delete:
                    mySharedPreferences.deletePrefs(Constants.REMOVE_CTAGURY,MainActivity.context);
                default:
            }
            return false;
        }
    }

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

...