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

前端 未结 2 686
醉梦人生
醉梦人生 2020-12-20 22:09

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 scro

相关标签:
2条回答
  • 2020-12-20 22:30

    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.

    0 讨论(0)
  • 2020-12-20 22:54

    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);
            }
        }
    }
    }
    
    0 讨论(0)
提交回复
热议问题