wasabeef / recyclerview-animators

An Android Animation library which easily add itemanimator to RecyclerView items.
Apache License 2.0
11.52k stars 1.98k forks source link

AlphaInAnimationAdapter and ScaleInAnimationAdapter work, others don't #14

Open pkliang opened 9 years ago

pkliang commented 9 years ago

Hi, I could only get the AlphaInAnimationAdapter and ScaleInAnimationAdapter work, others, for example SlideInRightAnimationAdapter, do not work.

wasabeef commented 9 years ago

@pkliang

Hi.

Exception occurred?

fabiendem commented 8 years ago

Digging up this issue as I have the same problem. SlideInLeftAnimationAdapter & SlideInRightAnimationAdapter won't work at least on the first load/display of the data. So if you update the data on your adapter and call notifyDataSetChanged(), everything is shown but without any animation.

It looks like the root cause is the call view.getRootView().getWidth() which returns 0 in

public class SlideInRightAnimationAdapter [...]

  @Override protected Animator[] getAnimators(View view) {
    return new Animator[] {
        ObjectAnimator.ofFloat(view, "translationX", view.getRootView().getWidth(), 0)
    };
  }

I am not sure yet how to fix this so that the width is valid. I am using the version 2.2.2.

fabiendem commented 8 years ago

Got a solution but it may be dirty. In AnimationAdapter.java, method onBindViewHolder, by wrapping the following lines in a post(), the items are animated at the first load.

for (Animator anim : getAnimators(holder.itemView)) {
   anim.setDuration(mDuration).start();
   anim.setInterpolator(mInterpolator);
}
mLastPosition = adapterPosition;

With the fix holder.itemView.post(new Runnable()...:

@Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        mAdapter.onBindViewHolder(holder, position);

        final int adapterPosition = holder.getAdapterPosition();
        if (!isFirstOnly || adapterPosition > mLastPosition) {
            holder.itemView.post(new Runnable() {
                @Override
                public void run() {
                    for (Animator anim : getAnimators(holder.itemView)) {
                        anim.setDuration(mDuration).start();
                        anim.setInterpolator(mInterpolator);
                    }
                    mLastPosition = adapterPosition;
                }
            });

        } else {
            ViewHelper.clear(holder.itemView);
        }
    }

Thanks to the post the itemView is at least partially rendered and has a proper width. I am not sure about the side effects of this though...

agueroveraalvaro commented 7 years ago

Well, i edited a little to leave from my problem, the onBindViewHolder must be :

@Override
 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
 { 
        mAdapter.onBindViewHolder(holder, position);
        int adapterPosition = holder.getAdapterPosition();
        for (Animator anim : getAnimators(holder.itemView))
        {
             anim.setDuration(mDuration).start();
             anim.setInterpolator(mInterpolator);
        }
        mLastPosition = adapterPosition;
    }

Thanks a lot for the great library!!

Adarshkl commented 6 years ago

Thank you. But how does this code help the other developers? These are gradle files & are non-editable. The issue of animation not showing on scroll up is still very much there for me.