0
 public void onRecyclerViewItemLongClicked(final int position, int id) {
    PopupMenu popup = new PopupMenu(MainActivity.this,binding.rcvPrefix.getChildAt(position));
    popup.getMenuInflater().inflate(R.menu.menu_item_rcv,popup.getMenu());
    popup.show();
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()){
                case R.id.item_add:
                    createDialogAdd();
                    initializeRCV();
                    Toast.makeText(MainActivity.this, "Add", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.item_edit:
                    Toast.makeText(MainActivity.this, "Edit", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.item_delete:
                    Toast.makeText(MainActivity.this, "Delete", Toast.LENGTH_SHORT).show();
                    db.deletePrefix(prefixList.get(position));
                    prefixList.remove(position);
                    initializeRCV();
                    break;
                default:
                    break;
            }
            return true;
        }
    });

}

//error

 E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.tiny.covertphonenum, PID: 13363
                  java.lang.IllegalStateException: MenuPopupHelper cannot be used without an anchor
                      at com.android.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:125)
                      at android.widget.PopupMenu.show(PopupMenu.java:218)
                      at com.example.tiny.covertphonenum.view.MainActivity.onRecyclerViewItemLongClicked(MainActivity.java:291)
                      at com.example.tiny.covertphonenum.presenter.adapter.AdapterRcvPrefix$1.onLongClick(AdapterRcvPrefix.java:49)
                      at android.view.View.performLongClick(View.java:5237)
                      at android.view.View$CheckForLongPress.run(View.java:21121)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I try show popup menu when long click item but error when item have position=10.please help me

Rahul Singh Chandrabhan
  • 2,531
  • 5
  • 22
  • 33

3 Answers3

0

Call getChildAt in RecyclerView to get the child item is not a proper way due to the way RecyclerView create/recycle the children base on scroll offset. To get the child item at a specific position, make sure that item is currently visible on screen (not recycled) or the result is null. Call this method:

ViewHolder viewHolder = rcvPrefix.findViewHolderForAdapterPosition(position)

Check for null before using the result viewHolder, then use viewHolder.itemView to be the anchor view.

Tam Huynh
  • 2,026
  • 1
  • 16
  • 20
0

Create interface like this

public interface OnLongClickListener {
    void onLongClick(int position);
}

and set to adapter of recyclerview like in constructor or by calling setmethod

and pass code in onbindview

holder.llParent.setTag(position);
holder.llParent.setOnLongClickListener(new View.OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
           int position = (Integer) v.getTag();
           onClickListener.onClick(position);
           return false;
      }
});

where llparent is linearlayout of parent layout of recyclerview's items..

Parth Suthar
  • 123
  • 4
0

Although quite a long time passed, I recently encountered this issue. In my case I had the same problem when clicking the button on last RecyclerView element for popup menu. It was half-visible so, apparently the Recyclerview has recycled it. The problem resolved by passing holder view to the popup menu initialize method. So, instead of

public void onRecyclerViewItemLongClicked(final int position, int id) {
PopupMenu popup = new PopupMenu(MainActivity.this,binding.rcvPrefix.getChildAt(position));
popup.getMenuInflater().inflate(R.menu.menu_item_rcv,popup.getMenu());
popup.show();
....

use

public void onRecyclerViewItemLongClicked(View view, final int position, int id) {
PopupMenu popup = new PopupMenu(MainActivity.this, view);
popup.getMenuInflater().inflate(R.menu.menu_item_rcv,popup.getMenu());
popup.show();
.....

So, you need to pass the view you clicked through your interface in addition to your data.

holder.llParent.setOnLongClickListener(new View.OnLongClickListener() {
  @Override
  public boolean onLongClick(View v) {
       int position = (Integer) v.getTag();
       onClickListener.onRecyclerViewItemLongClicked(v, position, id)
       return false;
  }
});

public interface OnLongClickListener {
void onRecyclerViewItemLongClicked(View v, int position, int id);
}
Taha
  • 117
  • 1
  • 11