简体   繁体   English

如何滚动到带有标题视图的ListView的顶部?

[英]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). 我找不到一种方法来以编程方式滚动到具有headerView的ListView的顶部(以便标题视图与列表项一起显示)。

I've tried, on the listView object: 我试过了,在listView对象上:

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. 一件事情确实起作用了, smoothScrollToPosition(0) ,但是它做的滚动动画不适合工作流程。 There is a listView.setSelectionAfterHeaderView() method, so it seems like listView.setSelection(0) should work... but it does not (it does the same thing). 有一个listView.setSelectionAfterHeaderView()方法,因此listView.setSelection(0)似乎可以工作...但它不起作用(它做同样的事情)。 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);
                    }
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:当ListView中没有/限制项目时,如何将标题滚动到顶部? - Android: How to scroll header to top when there is no/limited items in ListView? ListView:在滚动条顶部显示固定标题(或使用 dispatchDraw 在另一个顶部绘制视图) - ListView: show fixed header on top of scroll bar (or drawing a view on top of another with dispatchDraw) 如果 EditText 增长,如何使 ListView 视图在顶部自动滚动 - How to make ListView view auto scroll on top if EditText Grow 仅滚动ListView,并在顶部保留LinearLayout标头 - Scroll only the ListView and leave the LinearLayout header on top ListView - 滚动项目使其触及视图顶部? - ListView - scroll an item so it touches top of view? 滚动视图中的ListView我的滚动视图移到列表视图的顶部。 我该如何预防? - ListView in Scroll View my scrollview moves to top of listview. How do I prevent this? Android:在列表视图的顶部创建视图并与列表视图一起滚动 - Android: Make view on top of listview scroll together with the listview 将ListView Stick的标题视图(不是节标题)放在顶部 - Make Header View (not Section Header) of ListView Stick on Top 如何将视图滚动到屏幕顶部 - How to scroll view to Top of screen 如何在另一个滚动视图中滚动ListView - How to scroll ListView with in another scroll view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM