简体   繁体   中英

How to scroll to the top of a ListView with a header view?

I can't find a way to programmatically scroll to the top of a ListView that has a headerView (so that the header view is showing along with the list items).

I've tried, on the listView object:

setSelection(0)
setScrollY(0)
scrollTo(0,0)

To no avail. One thing does work, smoothScrollToPosition(0) , but it does a scroll animation that doesn't fit with the workflow. There is a listView.setSelectionAfterHeaderView() method, so it seems like listView.setSelection(0) should work... but it does not (it does the same thing). Any help is appreciated.

Try it:

            listView.post(new Runnable() {
                @Override
                public void run() {
                    if (Build.VERSION.SDK_INT >= 11) {
                        listView.smoothScrollToPositionFromTop(0, 0);
                    } else {
                        listView.setSelection(0);
                    }
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM