Lord_Gama
Lord_Gama

Reputation: 297

RecyclerView ItemAnimator animateAdd & animateChange both at the same time?

I'm trying to do this animation:

enter image description here

to insert a new item others move down as it does by default. but the first item and the second change. the first is inserted with ViewType 1 then the second switches the ViewType to 2. I currently manage 3 view types(adaptadorNotificaciones.java).

1 full size. notificacion_item1.xml

2 padding. notificacion_item2.xml

3 separator. section.xml

I extend from DefaultItemAnimator as follows (animadroNotificaciones):

public class animadroNotificaciones extends DefaultItemAnimator {

@Override
public boolean animateAdd(RecyclerView.ViewHolder holder){
    return true;
}

public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY){
    return super.animateChange(oldHolder,newHolder,fromX,fromY,toX,toY);
}

@Override
public void onAnimationFinished(RecyclerView.ViewHolder holder){
    super.onAnimationFinished(holder);
}


@Override
public boolean animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY, int toX, int toY){
    return super.animateMove(holder,fromX,fromY,toX,toY);
}}

I'm also picturing it like a stack:

LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    llm.setReverseLayout(true);
    llm.setStackFromEnd(true);
    recyclerView.setLayoutManager(llm);

so when I add an item the scroll has to move up:

recyclerView.scrollToPosition(arreglo.size()-1);

The whole code to insert an item (adaptadorNotificaciones.java):

public void addNotificacion(ItemLista notificacion,RecyclerView recyclerView){
    String mesAnterior = "";
    String mesInsertado = "";

    if(arreglo.size()!=0)
        mesAnterior = ManejadorFechas.getFecha(arreglo.get(arreglo.size()-1).getTiempo(),false);
    mesInsertado = ManejadorFechas.getFecha(notificacion.getTiempo(),false);

    if(!mesInsertado.equals(mesAnterior) && arreglo.size()!=0){

        arreglo.get(arreglo.size()-1).setTipo(2);
        arreglo.add(new ItemLista(3,mesAnterior));
        arreglo.add(notificacion);

        notifyItemRangeChanged(arreglo.size()-3,arreglo.size()-1);
        recyclerView.scrollToPosition(arreglo.size()-1);

    }else if(arreglo.size()!=0){
        arreglo.get(arreglo.size()-1).setTipo(2);
        arreglo.add(notificacion);
        notifyItemRangeChanged(arreglo.size()-2,arreglo.size()-1);
        recyclerView.scrollToPosition(arreglo.size()-1);
    }else{
        arreglo.add(notificacion);
        notifyItemInserted(arreglo.size()-1);
        recyclerView.scrollToPosition(arreglo.size()-1);
    }
}

but the above code gives me the next result:

enter image description here

I think It is caused by the following line:

notifyItemRangeChanged(arreglo.size()-3,arreglo.size()-1);

anyone has an idea how to fix this? Thanks in advance.

Upvotes: 2

Views: 1661

Answers (1)

Lord_Gama
Lord_Gama

Reputation: 297

I solved it, this way:

rv.setItemAnimator(new animadroNotificaciones(){
            @Override
            public void onAnimationFinished(RecyclerView.ViewHolder viewHolder) {
                adaptador.notifyDataSetChanged();
            }
        });

Upvotes: 1

Related Questions