RecyclerView animation when for replacing entire DataSet - android

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.

Related

Animate whole list using recycleView

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);

How to get whole scroll event when removing element from adapter of recyclerview?

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

Get RecyclerView to stop scrolling when I add items to the Adapter

How can I stop RecyclerView from scrolling to the bottom of the list whenever I add items?
Even when I insert something at the top of the list and call notifyDataSetChanged or notifyItemInserted the list scrolls to the bottom.
The one feature that works as expected is notifyItemRemoved(index)
I think I was calling notifyRangeInserted() with the entire list and that was scrolling to the end of that range.
If you're having similar issues, just cut the Adapter functionality back to its bare essentials and build up from there.
The problem was I had an item in the list, a ProgressBar to indicate that data was loading, but when the new items loaded I added them to the list, this moved the "focused item," the ProgressBar down and if there were enough items to move it off the screen then the RecyclerView would scroll down to keep it on screen. The problem being 1. I didn't want it to maintain "focus" and 2. The very next thing I did was remove the ProgressBar from the `Adapter.
So the solution is to remove the ProgressBar or other items before adding items earlier positions in the Adapter.

Animate RecyclerView additions from bottom

I am using the latest RecyclerView library to display a list with an option to add items to this list. The list is in reverse order, so newest additions are displayed at the bottom. When adding new items to a recycler view from the top, a nice animation is displayed and the list is automatically scrolled to display the newest item.
However, when adding new items from the bottom, items are not similarly pushed up as the new item is being animated. This leads to odd behavior where a new item is added and the animation cannot be seen unless the user scrolls to the bottom of the list.
As a temporary fix, I've forced the recycler view adapter to scroll to the first position after adding a new item. This isn't as seamless as adding from the top of a recycler view, is there a proper way to implement this?
The corresponding code is below:
la.addItem(0, msg);
layoutManager.scrollToPosition(0);
Add this function to your adapter class
public void addItem(Object aObject)
{
mAdapterList.add(aFormElement);
notifyItemInserted(mAdapterList.size()); // Passing position where insertion happen
}
If you are not getting animation than only use below function.
try to call scroll to position on recyclerview at your required position after calling addItem().

RecyclerView whole view animation when adding viewitem

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

Categories

Resources