Animate RecyclerView additions from bottom - android

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().

Related

RecyclerView scroll position

I have a RecyclerView includes 20 items from my rest api. When I add a new item to database, the first item in my list disappear and new one will add into the end of list by using mysql LIMIT 0,20
I want when I'm in the list and new item added, the scroll position doesn't change, so I used onSaveInstansceState before the adapter and onRestoreInstanceState after it. but as you can understand the new list has a difference with the older and that's the item position.So the list scroll one item more from its state.
How can I prevent this or is there another solution?
If you don't want the scroll position to change when adding an item, use notifyItemInserted().
// adapter
void addNewItem(Item item) {
itemList.add(item);
notifyItemInserted(itemList.size() - 1);
}

RecyclerView animation when for replacing entire DataSet

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.

How to avoid jerk when notifyDataSetChanged method of adapter is called?

I am trying to add pagination in my listView.
I added a method in my custom BaseAdapter which adds next page results at top of list on scroll to first item in list.
public void addEntriesToTop(List<ChatModel> entries) {
// Add entries to the top of the list
this.chatMessages.addAll(0, entries);
notifyDataSetChanged();
}
So if I have to add next page on top of ListView, I am using like this in my activity.
//result contain new items to be added to top of list
adapter.addEntriesToTop(result);
//so that list scroll sets to last visible message to user
final int index = result.size();
listView.post(new Runnable() {
#Override
public void run() {
//go to user's last scroll position
messagesContainer.setSelectionFromTop(index, 0);
}
});
This method is working fine and retains user's last scroll position. But it has a problem, when I call addEntriesToTop() it scrolls the ListView to bottom(because of notifyDataSetChanged() ) and then when I call setSelectionFromTop it scrolls to user's last position. In this transition, there is small jerk.
Please help me to smooth this transition.
Thanks
As ListView is old and now its obselete, now you can use Recyclerview
instead of ListView, here is the link that helps you to convert your
ListView to RecyclerView. RecyclerView have many methods to add your custom animations and also it have not any jerks while adding/removing items.
Replacing ListView with RecyclerView
For Default Animations you can use this link:
RecyclerView Animations – Add & Remove Items

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.

Add new items at top of ListView not working. Adds items to the bottom instead

I am facing some difficulties getting the addition of new items in a ListView to appear at the top of the ListView. However, when new items are added to the ListView, I want the current item to remain visible and the new items to be added above it.
To that effect, I followed the solution from here: https://stackoverflow.com/a/3035521/450534 and when that did not work too, I started experimenting a little.
Also worth mentioning is that I am using the Pull to Refresh library by Chris Banes.
Now curiously, when I add paged data below the last item in the ListView, all works well. But I am stumped why new items will not be placed above the current item and maintain the current position. This is what I have done so far (in the onPostExecute()):
// GET THE CURRENT LISTVIEW POSITION (USED TO MAINTAIN SCROLL POSITION)
int currentPosition = actualListView.getFirstVisiblePosition();
Log.e("POSITION", String.valueOf(currentPosition));
View v = actualListView.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
Log.e("TOP", String.valueOf(top));
/***** APPEND NEW DATA TO THE ADAPTER AND THE LISTVIEW *****/
// CAST THE ADAPTER (AGAIN)
adapter = new NewsFeedAdapter(getActivity(), arrFeeds);
// SET THE ADAPTER TO THE LISTVIEW
actualListView.setAdapter(adapter);
// SET THE SCROLL POSITION
actualListView.setSelectionFromTop(currentPosition, top);
lv.onRefreshComplete();
This is the start of the ListView and this is what should still be seen after adding new items at the top (above this). I plan to show a small layout that shows how many new feeds are on top. Something like the Facebook app does now.
But new items are added to the bottom of the ListView instead of at the top above existing items.
Is there something obvious that I have missed? If you need me to add more code, please tell me and I will add it immediately.
another alternative would be to add the item at 0 poisiton of the ArrayList which you are using to populate the adapter.
your_array_list.add(int index, Object element) //Inserts the specified element at the specified position in this list.
and after adding you can call the notifyDataSetChanged() on your listAdapter.
Please confirm if it works..

Categories

Resources