簡體   English   中英

當longclickitem recyclerView時在android中顯示彈出菜單

[英]show popup menu in android when longclickitem 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)

我嘗試在長按項目時顯示彈出菜單,但在項目具有 position=10 時出錯。請幫助我

由於RecyclerView基於滾動偏移創建/回收子項的方式,在RecyclerView調用getChildAt來獲取子項不是正確的方法。 要在特定位置獲取子項,請確保該項當前在屏幕上可見(未回收)或結果為空。 調用這個方法:

ViewHolder viewHolder = rcvPrefix.findViewHolderForAdapterPosition(position)

在使用結果viewHolder之前檢查 null,然后使用viewHolder.itemView作為錨視圖。

創建這樣的界面

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 項目的父布局的線性布局..

雖然已經過去了很長時間,但我最近遇到了這個問題。 在我的情況下,當單擊彈出菜單的最后一個 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM