简体   繁体   中英

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

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

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.

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)

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:

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

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