djk djk - 2 years ago 118
Android Question

Animation in recyclerview item by item

I need to implement animations in RecyclerView which has a slide up effect for every item. I have searched for this and tried many different things. It works when you scroll but it is not working when the page loads for the first time. I have uploaded a video on this link.



I have tried this code

@Override
public void onBindViewHolder(ViewHolderHelper holder, int position, List<Object> payloads) {
super.onBindViewHolder(holder, position, payloads);
setAnimation(holder.itemView, position);
}

private void setAnimation(View viewToAnimate, int position) {
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(mcontext, R.anim.slide_in_bottom_list_item);
animation.setInterpolator(new AccelerateInterpolator());
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}


Also tried

animation.setStartOffset(position * 100);


but that is missing an item on fast scrolling

Can anyone help me ?

djk djk
Answer Source

After searching on internet i decided to do a hacky solution.

animation.setStartOffset(position * 100);

Many post has this suggestion. But this is messing when you scroll fast in the recyclerview.

So i decided to do this offset for onlt first few visible items and then animation applies to other rows correctly without offset.

 @Override
    public void onBindViewHolder(ViewHolderHelper holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);

        setAnimation(holder.itemView, position);
    }

    private void setAnimation(View viewToAnimate, int position)
    {
        if (position > lastPosition)
        {
            Animation animation = AnimationUtils.loadAnimation(mcontext, R.anim.slide_in_bottom_list_item);
            animation.setInterpolator(new DecelerateInterpolator());
            if(position < 4)
                animation.setStartOffset(position * 200);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }

Hope this helps someone.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download