When using scrollTo, the ListView is not updated, but when manual scrolling is updated

I have different colors for different lines in the ListView, setting the color of the text field depending on the line number (in the getView () of the adapter). Now, when I manually scroll the ListView up, the correct color is displayed in the bottom lines that are displayed. But when I use scrollTo, this does not happen, all detected lines have the same color (they are not updated).

Has anyone encountered this problem? It seems incomprehensible!

+5
source share
2 answers

ListView#scrollTodoes not scroll the contents of the list. (This is the standard View method, not list-specific at all: it scrolls the ListView itself.)

ListView#setSelectionFromTop(0, int y) .

API 19+ ListView#scrollListBy(int y), KitKat .

+3

ListView#setSelectionFromTop(0, int y) . , :

class Hacks {

static {
    Method trackMotionScroll = null;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        try {
            trackMotionScroll = AbsListView.class
                    .getDeclaredMethod("trackMotionScroll", int.class, int.class);
            trackMotionScroll.setAccessible(true);
        } catch (NoSuchMethodException e) {
            Exceptions.crash(e);
        }
    }
    listViewTrackMotionScroll = trackMotionScroll;
}

public static void scrollListBy(ListView listView, int y) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        listView.scrollListBy(y);
    } else {
        try {
            listViewTrackMotionScroll.invoke(listView, -y, -y);
        } catch (Exception e) {
            Exceptions.crash(e);
        }
    }
}
}
0

All Articles