I have a RecyclerView.Adapter where list items are being removed, added and/or updated dynamically in the background, and the individual effects for this actions also work using notifyItemRemoved, notifyItemInserted and notifyItemChanged. But I have the problem that there is the case where e.g. an item is added dynamically to the end of the list at a position not currently visible in screen and in this special scenario I would like to shortly animate the complete list, so the user can know that the list changed, is there some specific guidelines for doing so, or some example how to handle this appropriately? I tried calling notifiyDataSetChanged after calling e.g. notifyItemRemoved but this cancels the effect of notifyItemRemoved and there isn't really a visible feedback.
I'm not sure which kind of animation you'd like to achieve, but something simple is: pass as a parameter to the Adapter the RecyclerView reference and do the animation there after you notifyItemInserted, e.g. scroll the RecyclerView's Layout to the end to show the user the new item using recyclerView.getLayoutManager().scrollToPosition(youPositionInTheAdapter)
Ohh and for animating the items you can also use this library https://github.com/wasabeef/recyclerview-animators
Update
In order to set a "flash" animation to the whole RecyclerView you could use after you notifyItemInserted
Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(500);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(2);
animation.setRepeatMode(Animation.REVERSE);
LayoutAnimationController controller = new LayoutAnimationController(myAnim, 0.01f);
recyclerView.setLayoutAnimation(controller);
Related
I would like to show a custom animation to match the other animations that I have in my RecyclerView. I can’t find a straight forward way of adding custom animations for removal. I know that when RecyclerView replaced ListView one of the talking points was the ability to know when items are added or removed rather than simply saying the dataset has changed and updating everything, so I figured there would be an easy way to add an animation to a View when it is removed that I am missing.
When I add an animation to a view that is removed the animation will not play. This is likely because the View is removed and the animation stops. Is there an easy way to add an animation to a View that doesn’t get cut off? I'm assuming that I could use a second thread, but I want to know if I am missing something.
//code for removal
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//animation to be added here on view
list.remove(holder.getAdapterPosition());
notifyItemRemoved(position);
notifyItemRangeChanged(position,list.size()-position);
}
});
All add/change/delete/move animations in recyclerView are made by ItemAnimator (by default animator is DefaultItemAnimator). So if you need custom delete animation you should provide custom ItemAnimator via recyclerView.setItemAnimator(animator) method.
I suggest to extend from SimpleItemAnimator and override animateRemove method.
So I was working with RecyclerView animations, and implemented an animation for when the items load into the List. I was wondering, if I can make an animation for when I replace the entire data set (an animation including the items joining and leaving the RecyclerView).
The correct way to remove an item from a recycler view is to remove the item from the data set and them inform the adapter that the item is removed. If you do this then the animation would happen similar to the animation that happens when you add an item.
myDataset.remove(position); // myDataset is List<MyObject>
mAdapter.notifyItemRemoved(position);//this line is important
If you are not using the second line then...
mAdapter.notifyDataSetChanged()
will be called and the animation will stop.
I'm trying to create "sticky headers" on a RecyclerView (which uses a LinearLayoutManager) but now I'm facing a problem. When I click on one item of the list, a new item is automatically inserted just below it and the RecyclerView's default animation is shown (the one which smoothly adds and shows the new item). When I tap the initial item again, this new added item is removed, and the RecyclerView's default animation is shown again (the one to slowly hide the removed item).
I have completed all the sticky headers functionality but now my problem is that when I click the last list item and the hide default animation is shown (with a scroll up), my sticky headers don't work. I need to know (and here is were I'm stuck) how to get notified while this RecyclerView animation is being performed when adding/removing items. I need to get notified with all the animation steps, not only the start and end ones.
So far I've tried to use a RecyclerView onScrollListener and a ViewTreeObserver (attached to the added/removed view) but neither worked. If someone could give me some help, it would be really appreciated.
Thanks in advance to all the Stack Overflow community
I have already know we can set item's animation by call RecyclerView's setItemAnimator method. After that, we can call notifyItemRangeRemoved to let all the items "fly away" together with animation, here is the remove code.
int removedSize = dataCollection.size();
dataCollection.clear();
notifyItemRangeRemoved(0, removedSize);
However, my requirement is to remove item one by one and with some delay. I have try to use Timer + TimerTask + Handler classes to achieve it, however, each time I called notifyItemRemoved method, the RecyclerView will auto refill the item which have already disappear.
How can remove one item, and the blank place will not be replaced with another item directly?
Try to use notifyDataSetChanged() instead of notifyItemRangeRemoved()
I faced a problem recently, as we know when we add a item to recycle view, we can set item animation when add,update or remove.
The item animation has two steps, it's separated.
Move old view items.
Start add item animation.
So question is, I need the whole recycle view execute the item animation or merge these two steps to one step, such as the new added item and old view item move up together.
The animation is automated and combine both of yours. You have to add the new item and call the notifyInsertItem method and after that the scrollToPosition method.
The animation should work fine