ensure visible on android listview? ensure visible on android listview? android android

ensure visible on android listview?


ListView.setSelection() will scroll the list so that the desired item is within the viewport.


Try it:

public static void ensureVisible(ListView listView, int pos){    if (listView == null)    {        return;    }    if(pos < 0 || pos >= listView.getCount())    {        return;    }    int first = listView.getFirstVisiblePosition();    int last = listView.getLastVisiblePosition();    if (pos < first)    {        listView.setSelection(pos);        return;    }    if (pos >= last)    {        listView.setSelection(1 + pos - (last - first));        return;    }}


I believe what you are looking for is ListView.setSelectionFromTop() (although I'm a bit late to the party).