在 android 中,在按钮所在的确切位置显示 recyclerView 中的弹出窗口

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

我正在 android.I 中使用带有 recyclerView 的 gridLayout 为每个网格项目提供一个选项,我想在其中显示我的弹出窗口 activity,这是另一个 class。请看图——

每个项目都有一个菜单选项。我的弹出 activity java 名称是 CustomPop.Class。我使用 recyclerView 来显示 gridView,它的 holder 方法就像

    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);

        }
    });
}

通过这个我可以显示弹出窗口activity。但问题是它出现在 activity 的中间,但我希望它在菜单选项旁边打开。

您应该使用弹出菜单来实现您想要的,如下所示:

创建菜单用户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>

将以上代码放入您的 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>

在你们各自的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;
                    }
                });
            }
        });

了解更多PopupMenu

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.

试试这个创建菜单文件

文件: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>  

不如像这样使用创建弹出菜单

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  

这里是示例演示链接how to create pop-up menu in android