[英]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.