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();
mListView.post(new Runnable() {
    @Override
    public void run() {
        mListView.setSelection(index);
    }
});

      

., 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 ...

[myListView.post(new Runnable() 
{
    @Override
    public void run() 
    {
        myListView.setSelection(pos);
        View v = myListView.getChildAt(pos);
        if (v != null) 
        {
            v.requestFocus();
        }
    }
});] 

      

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?

0


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 .

+5


source







All Articles