Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to prevent the listview from scrolling to its top position when its adapter's data is changed?

Tags:

I'm having a bit of trouble preserving the scroll position of a list view when changing it's adapter's data.

What I'm currently doing is to create a custom ArrayAdapter (with an overridden getView method) in the onCreate of a ListFragment, and then assign it to its list:

mListAdapter = new CustomListAdapter(getActivity());
mListAdapter.setNotifyOnChange(false);
setListAdapter(mListAdapter);

Then, when I receive new data from a loader that fetches everything periodically, I do this in its onLoadFinished callback:

mListAdapter.clear();
mListAdapter.addAll(data.items);
mListAdapter.notifyDataSetChanged();

The problem is, calling clear() resets the listview's scroll position. Removing that call preserves the position, but it obviously leaves the old items in the list.

What is the proper way to do this?

like image 876
urandom Avatar asked Feb 05 '13 22:02

urandom


People also ask

How do I stop list view from scrolling?

Just call stopScroll(myListView); when you need to stop scroll. Show activity on this post. // Stop scrolling smoothScrollBy(0, 0);

Is List view scrollable by default?

ListView itself is scrollable.

How do I make list view scroll smoothly?

The key to a smoothly scrolling ListView is to keep the application's main thread (the UI thread) free from heavy processing. Ensure you do any disk access, network access, or SQL access in a separate thread. To test the status of your app, you can enable StrictMode .


2 Answers

As you pointed out yourself, the call to 'clear()' causes the position to be reset to the top.

Fiddling with scroll-position, etc. is a bit of a hack to get this working.

If your CustomListAdapter subclasses from ArrayAdapter, this could be the issue:

The call to clear(), calls 'notifyDataSetChanged()'. You can prevent this:

mListAdapter.setNotifyOnChange(false); // Prevents 'clear()' from clearing/resetting the listview
mListAdapter.clear();
mListAdapter.addAll(data.items);
// note that a call to notifyDataSetChanged() implicitly sets the setNotifyOnChange back to 'true'!
// That's why the call 'setNotifyOnChange(false) should be called first every time (see call before 'clear()').
mListAdapter.notifyDataSetChanged(); 

I haven't tried this myself, but try it :)

like image 115
Streets Of Boston Avatar answered May 04 '23 05:05

Streets Of Boston


Check out: Maintain/Save/Restore scroll position when returning to a ListView

Use this to save the position in the ListView before you call .clear(), .addAll(), and . notifyDataSetChanged().

int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();

After updating the ListView adapter, the Listview's items will be changed and then set the new position:

mList.setSelectionFromTop(index, top);

Basically you can save you position and scroll back to it, save the ListView state or the entire application state.

Other helpful links:

Save Position: How to save and restore ListView position in Android

Save State: Android ListView y position

Regards,

Please let me know if this helps!

like image 27
Jared Burrows Avatar answered May 04 '23 05:05

Jared Burrows