简体   繁体   中英

Recycler view. Animate each item on create

I want to animate each view with delay. Curently all item are animated together on create. and only when i scroll the new added items are animated so i want to animate the initilly created items in the recycler view this is my code in my recycler view adapter

 private void setAnimation(View viewToAnimate, int position)
    {
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition)
        {
            Animation animation = AnimationUtils.loadAnimation(context, R.anim.slide_left);
            animation.setDuration(1000);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }

 @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {

        Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/gothamlight.ttf");
        NavigationData navigationData = arrayList.get(position);
        holder.NavigationImage.setImageResource(navigationData.getImg_res());
        holder.NavigationTitle.setText(navigationData.getNavigationTitle());
        holder.NavigationTitle.setTypeface(tf);
        setAnimation(holder.Container, position);

    }

try increasing the duration in your animation file ie slide_left in your case.

slide_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="1000"
        android:fromXDelta="100%"
        android:toXDelta="0%" >
    </translate>
</set>

我发现的解决方案是使用默认动画持续时间声明一个持续时间int,而内部设置的动画中我的持续时间=持续时间+300。而且效果很好。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM