如何在 firebaseui 提供的 recyclerview 项目上使用上下文菜单

how to use context menu on recyclerview item provided by firebaseui

我想知道如何将上下文菜单放在 FirebaseUI 提供的 recyclerview 项目中,并能够在知道长按项目的情况下执行一些操作。因为当我尝试在我的支架 class 上使用 onCreateContextMenu 时,我的 ContextMenuInfo 带有空值。 这是我的支架 class:

public class PlayerHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
    //private static final String TAG = PlayerHolder.class.getSimpleName();
    public TextView playerName;
    public TextView playerAge;

    public PlayerHolder(View itemView) {
        super(itemView);
        playerName = (TextView) itemView.findViewById(R.id.player_name);
        playerAge = (TextView) itemView.findViewById(R.id.player_age);
        itemView.setOnCreateContextMenuListener(this);
    }

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0, 1, 0, "Edit");//groupId, itemId, order, title
        menu.add(0, 2, 0, "Share");
        menu.add(0, 3, 0, "Delete");
    }

    //@Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView .AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case 1:
                //editNote(info.id);
                return true;
            case 2:
                //shareNote(info.id);
                return true;
            case 3:
                //deleteNote(info.id);
                return true;
            default:
                //return super.onContextItemSelected(item);
                return false;
        }
    }
}

这是我的 firebaseui 回收适配器 class

public class PlayerAdapter extends FirebaseRecyclerAdapter<Player, PlayerHolder> {

    private static final String TAG = PlayerAdapter.class.getSimpleName();
    private Context context;

    public PlayerAdapter(Class<Player> modelClass, int modelLayout, Class<PlayerHolder> viewHolderClass, DatabaseReference ref, Context context) {
        super(modelClass, modelLayout, viewHolderClass, ref);
        this.context = context;
    }

    @Override
    protected void populateViewHolder(PlayerHolder viewHolder, Player model, int position) {

        viewHolder.playerName.setText(model.getName());
        viewHolder.playerAge.setText(String.valueOf(playerAge) + " Years");
    }
}

我已经通过在这个 post 中使用这个解决方案解决了这个问题: How to create context menu for recycleview

这是我的代码:

MessageViewHolder Class:

public static class MessageViewHolder extends RecyclerView.ViewHolder  implements View.OnCreateContextMenuListener {
        public TextView tvMessage;
        public TextView tvDateSending;
        public CircleImageView ivMessenger;
        public LinearLayout linearLayout;


        public MessageViewHolder(View v) {
            super(v);
            tvMessage = (TextView) itemView.findViewById(R.id.tvMessage);
            tvDateSending = (TextView) itemView.findViewById(R.id.tvDateSending);
            ivMessenger = (CircleImageView) itemView.findViewById(R.id.ivMessenger);
            linearLayout= (LinearLayout) itemView.findViewById(R.id.llOneToOneChat);
            v.setOnCreateContextMenuListener(this);

        }
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            menu.add(0,1 , 0, "Edit");
            menu.add(0, COPY, 0, "Copy");
            menu.add(0, DELETE, 0, "Delete");
        }

    }

MessageAdapter class:

public class MessageAdapter extends FirebaseRecyclerAdapter<Message, MessageViewHolder>{

        private Context context;
        private long prevMessageTime=0;
        public int position;

        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }

        public MessageAdapter(Class<Message> modelClass, int modelLayout, Class<MessageViewHolder> viewHolderClass, DatabaseReference ref, Context context) {
            super(modelClass, modelLayout, viewHolderClass, ref);
            this.context = context;
        }

        @Override
        protected void populateViewHolder(MessageViewHolder viewHolder, Message message, final int position) {
            //do something
            viewHolder.tvMessage.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    setPosition(position);
                    return false;
                }

            });
        }
    }

最后在 Fragment/Activity 覆盖 onContextItemSelected 如下:

@Override
    public boolean onContextItemSelected(MenuItem item) {
        int position = -1;
        try {
            position = messageAdapter.getPosition();
        } catch (Exception e) {
//            Log.d(TAG, e.getLocalizedMessage(), e);
            return super.onContextItemSelected(item);
        }
        switch (item.getItemId()) {
            case 1:
                // do your stuff

                break;
            case 2:
                // copy message
                if (position!= -1){
                    ClipboardManager clipboard = (ClipboardManager) OneToOneConversationActivity.this.getSystemService(Context.CLIPBOARD_SERVICE);
                    String text = messageAdapter.getItem(position).getMessage();
                    ClipData clip = android.content.ClipData.newPlainText("Copied Text", text);
                    clipboard.setPrimaryClip(clip);

                }

                break;
            case 3:
                //delete message
                if (position!= -1)
                    messageAdapter.getRef(position).removeValue();
                break;
        }
        return super.onContextItemSelected(item);
    }

希望对你有用!