在removeValue时,Firebase Recycler Adapter不显示视图属性

时间:2016-10-28 10:08:31

标签: android firebase android-recyclerview firebase-realtime-database firebaseui

我尝试使用以下代码删除Firebase Recycler Adapter中的项目:

messageAdapter.getRef(position).removeValue();

但在“回收”视图中,此项目的视图搞砸了: enter image description here

以下是我的Firebase Recycler Adapter代码。当我删除项目时它工作正常。 MessageViewHolder 类:

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 类:

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

            });
        }
    }

在“活动”中,我覆盖 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);
    }  

如何让该视图从屏幕上消失?

1 个答案:

答案 0 :(得分:0)

我找到了可以将删除的视图从RecyclerView中删除的技巧: 这是我的方法:

{{1}}
带有linearlayout的

视图包含此viewHolder中的所有组件!