当longclickitem recyclelerView显示android中的弹出菜单

时间:2018-05-31 04:06:43

标签: android android-recyclerview

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

}

//错误

 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)

我在长按项目时尝试显示弹出菜单,但是当项目有位置= 10.请帮助我

时出错

3 个答案:

答案 0 :(得分:0)

getChildAt中调用RecyclerView以获取子项不正确,因为RecyclerView基于滚动偏移创建/回收子项的方式。要将子项目放在特定位置,请确保该项目当前在屏幕上可见(未回收)或结果为空。调用此方法:

ViewHolder viewHolder = rcvPrefix.findViewHolderForAdapterPosition(position)

在使用结果viewHolder之前检查null,然后使用viewHolder.itemView作为锚点视图。

答案 1 :(得分:0)

像这样创建界面

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

并在构造函数中或通过调用setmethod

设置为recyclerview的适配器

并在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;
      }
});

其中llparent是recyclerview项目的父布局的linearlayout ..

答案 2 :(得分:0)

虽然过去了很长时间,但我最近遇到了这个问题。在我的情况下,当单击弹出菜单的最后一个 RecyclerView 元素上的按钮时,我遇到了同样的问题。它是半可见的,显然 Recyclerview 已经回收了它。通过将持有者视图传递给弹出菜单初始化方法解决了问题。所以,而不是

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

使用

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

因此,除了您的数据之外,您还需要通过您的界面传递您点击的视图。

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);
}
相关问题