When using scrollTo ListView does not refresh, but when manually scrolling it refreshes

◇◆丶佛笑我妖孽 提交于 2019-12-29 08:43:30

问题


I have different colors for different lines in the ListView, by setting the color of textbox depending on the line number (in getView() of Adapter). Now when I manually scroll the ListView upwards the correct color is displayed in the bottom lines that get revealed. But when I use scrollTo, this doesnot happen, all lines revealed have the same color (they are not updated).

Has anybody faced this issue? This seems baffling!


回答1:


ListView#scrollTo doesn't scroll the list contents. (It's a standard View method, and not specific to lists at all: it scrolls the ListView view itself.)

Instead, try using ListView#setSelectionFromTop(0, int y) to scroll.

API 19+ has a ListView#scrollListBy(int y) method if you're programming for KitKat and up.




回答2:


In our project ListView#setSelectionFromTop(0, int y) does not have any effect. So we hacked this in compat-style:

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);
        }
    }
}
}


来源:https://stackoverflow.com/questions/14923055/when-using-scrollto-listview-does-not-refresh-but-when-manually-scrolling-it-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!