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

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

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

NotifyDataSetChanged refreshes RecyclerView but scrolls as well.

I have a RecyclerView and one item row needs to be updated like every second (SeekBar). So I call NotifyDataSetChanged once I update the data in the list. UI gets updated but the issue is that the RecyclerView scrolls so that this particular item is either at top or bottom of the screen.
I don't want RecyclerView to scroll.
// Update Data
mData.set(mData.indexOf(cardData), cardData);
// RefreshView
refreshView() {
runOnUiThread(new Runnable(){
notifyDataSetChanged();
}
);
You can update one item using notifyItemChanged(position); and u wont update all data in adapter, you will update only certain item
It's better to use the other notify methods from the RecyclerView.
For your usecase only swap out one item in your Adapter instead of all items and use notifyItemChanged(position).
This will be faster, the View won't scroll and if you ever will add an animation this will only work if you don't use notifyDataSetChanged
I got it working. I was having a card view above the recycler view.I just removed it. Now the whole screen is a recyclerView and I cannot see any auto scrolls.

How to programmatically initiate a swipe to dismiss with RecyclerView

When I delete an item from a RecyclerView I want to show a "swipe" animation, that is that the item is moved sideways off the screen. I believe there is support for "swipe to dismiss" using ItemTouchHelper, but this is touch-initiated whereas I want to be able to initiate the swipe programmatically.
I have also tried setting the RecyclerView item animator by extending DefaultItemAnimator. Using this method I can get items to swipe left and right but unfortunately the gap in the list closes quickly such that the swipe does not finish before the list item gap has closed.
Anyone know how to do this?
You can use ItemAnimators offered by this library. More specifically, your would use their SlideInLeftAnimator or SlideInRightAnimator.
Although it's already answered with external library; probably someone wants to do it with raw java.. I have done it with the solution in here
The idea is that, you take off the list item view of the RecyclerView to be deleted and animate it; and normally remove it from the adapter
private void removeListItem(View rowView, final int position) {
Animation anim = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
anim.setDuration(500);
rowView.startAnimation(anim);
new Handler().postDelayed(new Runnable() {
public void run() {
values.remove(position); //Remove the current content from the array
adapter.notifyDataSetChanged(); //Refresh list
}
}, anim.getDuration());
}

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

Android: scroll to the bottom of the view

I have a ListView containing some custom Views, and at the end of my list, a FooterView containing a Button.
When I click the button, it adds a view at the end of my ListView. But the cursor of the scrollbar stays at its position.
Now, what I want is to auto-scroll to the end of the list (so the user can see the new item).
Do you have any idea on how I can do that?
i think you will find you happyness here
ListView has a method just for this: smoothScrollToPosition
Once you've added the new item, just calculate the length of the list and pass that (remembering zero-indexing) to the above method
Try this one.. it will solve your problem, i tried it and it works great.
listView.post(new Runnable(){
public void run() {
listView.setSelection(listView.getCount() - 1);
}});

Categories

Resources