android ListView : scrollTo doesn't work

Alexis picture Alexis · Sep 19, 2012 · Viewed 7k times · Source

I have a view that contains a ListView which is binded to a cursor adapter. When The cursor content change I want to keep the ListView at the top then in my custom cursor adapter I added :

@Override
protected void onContentChanged() {
    // ...
    myListView.scrollTo(0, 0);
}

but this doesn't work. Then I read somewhere to queue this action like this :

myListView.post(new Runnable() {
    public void run() {
        myListView.scrollTo(0, 0);
    }
});

but this doesn't work either.

How can I keep the ListView at the top when its content changes?

EDIT:

Just for try, I added a button and called scrollTo() in its onClickListener and it didn't work! What am I missing ?

Answer

Eldhose M Babu picture Eldhose M Babu · Sep 19, 2012

Instead of scrollTo, try setSelection(0) to get to the top position of list view.