简体   繁体   English

如何为 Recyclerview 的特定项目设置动画?

[英]How to animate specific item of Recyclerview?

This is a Image of Recycler Item:这是回收商项目的图片: 在此处输入图片说明

I want when i click on plus button new item added with animation Here is a code我想要当我点击加号按钮时添加了动画的新项目这是一个代码

viewHolder.cart_plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            menu.add(position+1,menu.get(position));
            Singleton.getInstance().getMenuExtraArrayList().add(position+1,menu.get(position));
            ((CartActivity)context).cartIconCounter.setText( Singleton.getInstance().getMenuExtraArrayList().size()+"");
            ((CartActivity)context).setPrice();
            notifyDataSetChanged();
            setAnimation(position,viewHolder.itemView);

        }
    });

and here is setAnimationMethod这是 setAnimationMethod

private void setAnimation(int psition, View itemView) {
    if (psition > lastPosition)
    {
        Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
        itemView.startAnimation(animation);
        lastPosition = psition;
    }
}

and initially value of lastposition is -1并且 lastposition 的初始值为 -1

What you're doing here by calling notifyDataSetChanged() is telling the recycler view that all of your dataset has been changed and they should be redrawn, while you actually need just the added view at position+1 to be inserted.您在这里通过调用notifyDataSetChanged()所做的是告诉回收器视图您的所有数据集都已更改并且应该重新绘制它们,而实际上您只需要插入position+1处的添加视图。

To only notify the recycler view about the inserted item you can just call notifyItemInserted(position+1) (assuming that the list item index and the recycler view item position are identical)要仅通知回收站视图有关插入的项目,您只需调用notifyItemInserted(position+1) (假设列表项索引和回收站视图项目位置相同)

You might also be interested to learn a neat way to implement RecyclerView animations here , which uses the built-in android:layoutAnimation attribute to define your layout insertion animations, ie:您可能也有兴趣在此处学习实现 RecyclerView 动画的简洁方法,它使用内置的android:layoutAnimation属性来定义您的布局插入动画,即:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"                                        
    android:layoutAnimation="@anim/layout_animation_fall_down"
    />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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