Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PopupMenu is not positioned properly inside RecyclerView

I'm making an Android app to load data into a RecyclerView with an ImageView inside as the overflow button. When users click it, a PopupMenu is displayed with options to choose. I had managed to show it but the position does not seem right. Please look at the screenshots.

Only the first two items seem OK. Here are the source code:

view.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    final PopupMenu popupMenu = new PopupMenu(context, view);
    final Menu menu = popupMenu.getMenu();

    popupMenu.getMenuInflater().inflate(R.menu.menu_item_action, menu);
    popupMenu.setOnMenuItemClickListener(onMenuItemClickListener);

    switch (Global.listMode) {
      case Global.LIST_STYLE_NORMAL: {
        menu.findItem(R.id.action_delete).setVisible(false);
        break;
      }
      case Global.LIST_STYLE_FAVORITE: {
        menu.findItem(R.id.action_add_to_favorite).setVisible(false);
        break;
      }
      case Global.LIST_STYLE_WATCH_LIST: {
        menu.findItem(R.id.action_add_to_watch_list).setVisible(false);
        break;
      }
      case Global.LIST_STYLE_DOWNLOAD: {
        menu.findItem(R.id.action_download).setVisible(false);
        break;
      }
    }

    itemPosition = (int) view.getTag(R.id.tag_item_position);
    popupMenu.show();
  }
});

Can you guys point me out the issue, I had took hours for searching but still found nothing up now.

P/S: The clicked button is marked with red circle.

Any helps would be appreciated!

like image 674
ByulTaeng Avatar asked May 19 '15 04:05

ByulTaeng


2 Answers

Luckily, I've just found the way to solve the problem:

  • Use android.widget.PopupMenu not android.support.v7.widget.PopupMenu
  • Replace final PopupMenu popupMenu = new PopupMenu(context, view); with final PopupMenu popupMenu = new PopupMenu(context, v);

Honestly, I don't know why, just try it and voila!

like image 121
ByulTaeng Avatar answered Oct 14 '22 16:10

ByulTaeng


I'm not pretty sure but seems that a similar problem is duscussed here and it already has an accepted answer. Hope it will help you with your problem. Just stumbled upon your question and saw the topic discussed in the link and thought maybe it might be helpful for you too.

like image 39
ZanderEso Avatar answered Oct 14 '22 17:10

ZanderEso