PopupMenu在recycleview中无法正常工作

时间:2017-12-27 13:35:30

标签: android android-recyclerview

   holder.iv_delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final PopupMenu popupMenu = new PopupMenu(context,  holder.iv_delete);
            MenuInflater inflater = popupMenu.getMenuInflater();
            inflater.inflate(R.menu.menu_Popup, popupMenu.getMenu());
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.action_alert:
                           //perform operstion
                            return true;
                     case R.id.action_delete:
                          //perform operstion
                            return true;
                    }

                    return false;
                }
            });


            popupMenu.show();
        }
    });

} 

当我点击红色圆圈弹出菜单中的删除按钮时,需要在该按钮侧打开但是打开的是上侧

image

1 个答案:

答案 0 :(得分:0)

试试这个

holder.iv_delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    Context wrapper = new ContextThemeWrapper(context, R.style.popupMenuStyle);
    PopupMenu popupMenu = new PopupMenu(wrapper, v, Gravity.END);

    popupMenu.inflate(R.menu.menu_Popup);

    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.action_alert:
                           //perform operstion
                            return true;
                     case R.id.action_delete:
                          //perform operstion
                            return true;
                    }

                    return false;
                }
            });


    popupMenu.show();
        }
    });

styles.xml

<style name="popupMenuStyle" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:textColor">#000000</item>
        <item name="android:itemBackground">#FFFFFF</item>
</style>