Android ListView 滚动到顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2889793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ListView scrolling to top
提问by aakash
I have a ListView
with custom rows. When any of these rows is
clicked, the ListView's data is regenerated. I'd like the list to
scroll back to the top when this happens.
我有一个ListView
自定义行。单击这些行中的任何一行时,都会重新生成 ListView 的数据。发生这种情况时,我希望列表能够滚动到顶部。
I initially tried using setSelection(0)
in each row's OnClickListener
to achieve this but was unsuccessful (I believe because the ListView
loses its scroll position when its data is invalidated - so my call to
setSelection is undone. I still don't understand how the ListView
decides where to scroll to after invalidation, though).
我最初尝试setSelection(0)
在每一行中使用OnClickListener
来实现这一点,但没有成功(我相信是因为ListView
当它的数据无效时它会丢失它的滚动位置 - 所以我对 setSelection 的调用被撤消了。我仍然不明白 ListView 如何决定滚动到哪里无效后,虽然)。
The only working solution I know of was given by Romain Guy here: http://groups.google.com/group/android-developers/browse_thread/thread/127ca57414035301
我所知道的唯一可行的解决方案是 Romain Guy 在这里给出的:http: //groups.google.com/group/android-developers/browse_thread/thread/127ca57414035301
It involves (View.post
)ing the call to _listView.setSelection(0)
. I
found this to perform quite poorly.
The newly generated list shows up with its scroll location unchanged
and there is a considerable delay before it scrolls back to the top.
它涉及 ( View.post
) 调用_listView.setSelection(0)
。我发现这表现很差。新生成的列表显示时其滚动位置不变,并且在滚动回顶部之前有相当长的延迟。
Is there any better way to achieve this functionality?
有没有更好的方法来实现这个功能?
Any help would be much appreciated.
任何帮助将非常感激。
Thanks!
谢谢!
回答by D. B.
call listView.setSelectionAfterHeaderView();
to scroll to top
调用listView.setSelectionAfterHeaderView();
滚动到顶部
回答by N20084753
I have tried lot but this one worked for me
我尝试了很多,但这个对我有用
list.smoothScrollToPosition(0);
回答by radhoo
I simply use listview.setSelection(0);
我只是用 listview.setSelection(0);
Works fine for me.
对我来说很好用。
回答by Dmitry Polomoshnov
If you need instant scroll just after ListView
adapter's data was changed, pay attention that it might not be yet populated. In this case you should post()
your setSelection()
or setSelectionAfterHeaderView()
via Handler
so it will be called later in the queue.
如果您需要在ListView
适配器数据更改后立即滚动,请注意它可能尚未填充。在这种情况下,您应该使用post()
您的setSelection()
或setSelectionAfterHeaderView()
通过的,Handler
以便稍后在队列中调用它。
listView.Handler.post(new Runnable() {
@Override
public void run() {
listView.setSelectionAfterHeaderView();
}
});
This worked for me.
这对我有用。
回答by CommonsWare
Personally, I recommend you find a different UI pattern. It is possible that users will find your current "click, and the list changes in situ" approach intuitive, but I am skeptical.
就个人而言,我建议您找到不同的 UI 模式。用户可能会发现您当前的“单击,列表就地更改”的方法很直观,但我对此表示怀疑。
You could try subclassing ListView
and overriding layoutChildren()
to chain to the superclass, then call setSelection(0)
in the case where that is needed. If the "considerable delay" is due to just the post()
call, this should clear it up.
您可以尝试子类化ListView
和覆盖layoutChildren()
以链接到超类,然后setSelection(0)
在需要时调用。如果“相当大的延迟”只是由于post()
通话造成的,这应该可以解决。
回答by shaobin0604
as a workaround, you can create a new adapter containing the new regenerated data, then call ListView.setAdapter. after that call ListView.setSelection(n).
作为一种解决方法,您可以创建一个包含新的重新生成数据的新适配器,然后调用 ListView.setAdapter。之后调用 ListView.setSelection(n)。
btw, the solution provided by commonsware is worked.
顺便说一句,commonsware 提供的解决方案是有效的。
回答by Fahad
On some different requirement. If you want to scroll up just like while chatting.
在一些不同的要求。如果你想像聊天一样向上滚动。
mListView.smoothScrollToPosition(mAdapter.getCount());
回答by Sampath Kumar
This one worked fine when you want to focus the edittext from listview header
当您想从列表视图标题集中编辑文本时,这个工作正常
listview.setSelectionFromTop(0,0);
If you want to select the particular index view from listview then
如果要从列表视图中选择特定的索引视图,则
listview.setSelection(index); // o for top