Show popup in recyclerView exactly where the button is, in android

后端 未结 2 923
情书的邮戳
情书的邮戳 2021-01-25 12:44

I\'m working on a gridLayout with recyclerView in android.I\'ve a an option with each grid Item where I want to show my popup activity which is another class. Please see the im

相关标签:
2条回答
  • 2021-01-25 13:00

    You should have to use popup menu to achieve what you want just as follow :

    Create a menu user res > menu > your_menu.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:id="@+id/menu_item_one"
            android:title="Item One" />
        <item
            android:id="@+id/menu_item_two"
            android:title="Item Two" />
    </menu>
    

    Put above code into your style.xml

    <style name="popupMenuStyle" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="android:textColor">@color/colorBlack</item>
            <item name="android:itemBackground">@color/colorWhite</item>
            <!--<item name="android:popupBackground">@android:color/white</item>-->
        </style>
    

    In your respective RecyclerView Adapter

    holder.myAlbumsOptionButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    Context wrapper = new ContextThemeWrapper(context, R.style.popupMenuStyle);
                    final PopupMenu popup = new PopupMenu(wrapper, v, Gravity.END);
                    popup.inflate(R.menu.your_menu);
    
                    popup.show();
    
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            switch (item.getItemId()) {
                                case R.id.menu_item_one:
    
                                    //Do operation if menu_item_one
    
                                    break;
                                case R.id.menu_item_two:
    
                                    //Do operation if menu_item_two
    
                                    break;
                            }
                            return false;
                        }
                    });
                }
            });
    

    To know more about PopupMenu

    0 讨论(0)
  • 2021-01-25 13:18

    as per my above comment you can use Popup Menu

    Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu.

    try this create menu file

    file: poupup_menu.xml

    <menu xmlns:androclass="http://schemas.android.com/apk/res/android" >  
    
        <item  
            android:id="@+id/one"  
            android:title="One"/>  
    
        <item  
            android:id="@+id/two"  
            android:title="Two"/>  
    
        <item  
            android:id="@+id/three"  
            android:title="Three"/>  
    
    </menu>  
    

    than use create popup menu like this

    holder.img_chatroom_menu.setOnClickListener(new OnClickListener() {  
    
               @Override  
               public void onClick(View v) {  
                //Creating the instance of PopupMenu  
                PopupMenu popup = new PopupMenu(MainActivity.this, button1);  
                //Inflating the Popup using xml file  
                popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());  
    
                //registering popup with OnMenuItemClickListener  
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
                 public boolean onMenuItemClick(MenuItem item) {  
                  Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
                  return true;  
                 }  
                });  
    
                popup.show();//showing popup menu  
               }  
              });//closing the setOnClickListener method  
    

    here is the sample demo links how to create pop-up menu in android

    0 讨论(0)
提交回复
热议问题