Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView scrolling to top

I have a ListView with custom rows. When any of these rows is clicked, the ListView's data is regenerated. I'd like the list to scroll back to the top when this happens.

I initially tried using setSelection(0) in each row's OnClickListener to achieve this but was unsuccessful (I believe because the ListView loses its scroll position when its data is invalidated - so my call to setSelection is undone. I still don't understand how the ListView decides where to scroll to after invalidation, though).

The only working solution I know of was given by Romain Guy here: http://groups.google.com/group/android-developers/browse_thread/thread/127ca57414035301

It involves (View.post)ing the call to _listView.setSelection(0). I found this to perform quite poorly. The newly generated list shows up with its scroll location unchanged and there is a considerable delay before it scrolls back to the top.

Is there any better way to achieve this functionality?

Any help would be much appreciated.

Thanks!

like image 926
aakash Avatar asked May 22 '10 21:05

aakash


3 Answers

call listView.setSelectionAfterHeaderView(); to scroll to top

like image 88
D. B. Avatar answered Sep 23 '22 18:09

D. B.


I have tried lot but this one worked for me

list.smoothScrollToPosition(0);
like image 102
N20084753 Avatar answered Sep 22 '22 18:09

N20084753


I simply use listview.setSelection(0);

Works fine for me.

like image 42
radhoo Avatar answered Sep 24 '22 18:09

radhoo