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

泪湿孤枕 提交于 2019-12-02 12:42:04

问题


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 image -

There is a menu option with each Item. and my popup activity java name is CustomPop.Class. I use a recyclerView for showing gridViews and its holder method is like

    public void onBindViewHolder(ViewHolder holder, int position) {
    holder.img_chatroom_menu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(view.getContext(), ChatroomPopup.class);
            view.getContext().startActivity(intent);

        }
    });
}

By this I can show the popup activity. But the problem is It's appear in the middle of the activity but I want it to open beside the menu option.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/46440245/show-popup-in-recyclerview-exactly-where-the-button-is-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!