Programmatically set position in ListView without scrolling

This is for Android 4.4.2 tablet. I have a ListView with hundreds of items in it, and about 20 visible at the same time. User does not want smooth scrolling animation. How can I programmatically set the displayed position in a Listview without using the smoothScrollToPosition () function?

I searched Stack Overflow and in Android ListView setSelection () doesn't seem to work , they suggested this:

mListView.clearFocus(); Runnable() {
    public void run() {


., but it just sets the selection; it doesn't display that part of the ListView. setSelection () seems to be a popular solution all over the web, but I couldn't find anything in the documentation saying that setSelection () also sets the position, and it ONLY sets the selection and doesn't change the position on mine.

In Go to item in Listview without using smoothScrollToPosition they suggested Romain Guy's solution ...

[ Runnable() 
    public void run() 
        View v = myListView.getChildAt(pos);
        if (v != null) 


The problem with this is that my ListView is part of a ListActivity via a custom adapter getView (), so views that are not visible are reworked, that is, if I ask for a child view, it returns null on screen . Anyway, this is indeed the ListView I'm trying to control, so doing it indirectly through the child view seems terribly indirect.

How do I tell the ListView which part of it I want to see on screen?


source to share

1 answer

AbsListView has a method called smoothScrollToPositionFromTop()

and it takes a parameter duration

. So if you set it to 0, you can do it without scrolling, animating.

smoothScrollToPositionFromTop .



All Articles