My Will My Will - 9 months ago 92
Android Question

Firebase Recycler Adapter don't show view property when removeValue

I'm trying to delete an item in Firebase Recycler Adapter with this code:


but in Recycle view, the view of this item messed up:
enter image description here

Here is my code about Firebase Recycler Adapter. It works fine exept when i delete item.
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) {
tvMessage = (TextView) itemView.findViewById(;
tvDateSending = (TextView) itemView.findViewById(;
ivMessenger = (CircleImageView) itemView.findViewById(;
linearLayout= (LinearLayout) itemView.findViewById(;

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;

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


In Activity, I override the onContextItemSelected

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

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


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

how to make that view disappears from screen?

Answer Source

I found the trick that can take the deleted view away from RecyclerView: here is my method:

public void deleteMessage(int position){
    MessageViewHolder messageViewHolder= (MessageViewHolder) recyclerView.findViewHolderForAdapterPosition(position);


with linearlayout is the view contains all component in this viewHolder!