80

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!

8 Answers 8

135

call listView.setSelectionAfterHeaderView(); to scroll to top

1
  • 69
    that's exactly what I would've called it.
    – markshiz
    Oct 10, 2012 at 18:10
102

I have tried lot but this one worked for me

list.smoothScrollToPosition(0);
2
  • 3
    This is awesome. I wonder if you can change the scroll speed.
    – theblang
    Nov 14, 2013 at 22:37
  • Can there also be a callback to know when the scoll ends so that I can navigate to next screen ?
    – Amol Gupta
    Sep 9, 2014 at 6:53
42

I simply use listview.setSelection(0);

Works fine for me.

5

If you need instant scroll just after ListView adapter's data was changed, pay attention that it might not be yet populated. In this case you should post() your setSelection() or setSelectionAfterHeaderView() via Handler so it will be called later in the queue.

listView.Handler.post(new Runnable() {
            @Override
            public void run() {
                listView.setSelectionAfterHeaderView();    
            }
        });

This worked for me.

4

Personally, I recommend you find a different UI pattern. It is possible that users will find your current "click, and the list changes in situ" approach intuitive, but I am skeptical.

You could try subclassing ListView and overriding layoutChildren() to chain to the superclass, then call setSelection(0) in the case where that is needed. If the "considerable delay" is due to just the post() call, this should clear it up.

2
  • 4
    Thanks for the suggestion, I will give it a shot sometime soon. The list is changed in situ because breadcrumbs are shown elsewhere on the screen - the typical android UI designs are a less suited to my specific UI goals and target users. Also, thanks for ALL your help - I have read a number of your stackoverflow answers and your CommonsWare literature, and they have significantly helped me in getting started on this platform!
    – aakash
    May 23, 2010 at 0:25
  • this is the way to do it, no magical methods, just pure logic. Thanks! May 1, 2014 at 6:50
2

as a workaround, you can create a new adapter containing the new regenerated data, then call ListView.setAdapter. after that call ListView.setSelection(n).

btw, the solution provided by commonsware is worked.

2

On some different requirement. If you want to scroll up just like while chatting.

        mListView.smoothScrollToPosition(mAdapter.getCount());
2

This one worked fine when you want to focus the edittext from listview header

listview.setSelectionFromTop(0,0);

If you want to select the particular index view from listview then

listview.setSelection(index); // o for top

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.