Search code examples
androidandroid-listviewandroid-animationnotifydatasetchanged

Android notifyDataSetChanged() only the last added item


I have a ListView and its adapter class that extends BaseAdapter.

I want to animate a little custom partial that goes inside the listview.

I have the animation for the partial which is to slide from right to left - this works very good first time.

Within the method of the Adapter, similarly as shown, for brevity:

public View getView(){

    Animation an = AnimationUtils.loadAnimation(mContext, R.anim.transladar);
    an.reset();
    vi.startAnimation(an);
    return vi;
}

looks good, but when I add one more item chatAdapter.notifyDataSetChanged(); gets triggered which refreshes all my items and restarts the animation.

I only just want to animate the last item within the Adapter itself.

I hope you can help me.


Solution

  • You can add public Boolean variable (isFirstTime = true;) and in your getView() check it like this

    if(isFirstTime){
        Animation an = AnimationUtils.loadAnimation(mContext, R.anim.transladar);
        an.reset();
        vi.startAnimation(an);
    
       if(position == getCount()-1) isFirstTime =false;
    }else{
      if(position == getCount()-1){
         Animation an = AnimationUtils.loadAnimation(mContext, R.anim.transladar);
        an.reset();
        vi.startAnimation(an);
       }
    
    }
    

    Note: this is fast solution I don't know if there other way.

    Hope this helped you.