简体   繁体   English

禁用ListView滚动到顶部

[英]Disable ListView Scrolling to Top

I have a listview to which items are added dynamically to the top of the list. 我有一个listview,将项目动态添加到列表顶部。 I show some latest events occurring at the top of the list. 我显示了列表顶部发生的一些最新事件。 The problem is, these events occur very frequently, like 5 events in a second. 问题是,这些事件非常频繁地发生,例如每秒发生5个事件。 This makes the list go down. 这使列表下降。 Suppose user is looking at one event, then when new event occurs it pushes the event he was reading below, and that disappears. 假设用户正在查看一个事件,然后在发生新事件时,它将正在阅读的事件推到下面,然后消失。 I want the new events to be added at the top but it should be shown only when he scrolls up. 我希望将新事件添加到顶部,但仅当他向上滚动时才显示。 I call notifydatasetchanged on the adapter once new events occur and this pushes the list down. 一旦发生新事件,我就在适配器上调用notifydatasetchanged,这会将列表向下推。 Is there anyway I can freeze the scrolling. 无论如何,我可以冻结滚动。 I tried to disable the scrolling by setTranscriptMode. 我试图通过setTranscriptMode禁用滚动。 But it doesn't seem to help. 但这似乎没有帮助。 Kindly give me some hints and point me 请给我一些提示并指出我

What you can do is try to get the visible position of ListView on the screen using getFirstVisiblePosition() and then set the Adapter again with setSelection of position you got. 您可以做的是尝试使用getFirstVisiblePosition()获取ListView在屏幕上的可见位置,然后使用setSelection获得的位置再次设置Adapter。

int position = mListView.getFirstVisiblePosition();
mListView.setAdapter(adapter);
mListView.setSelection(position + 1);

I guess you would need (position + 1) because you are adding items on top of the List for every single item added to List. 我想您需要(位置+ 1),因为您要为添加到列表的每个单个项目在列表顶部添加项目。

To show latest events as you require you need to set android:transcriptMode="normal" This will by default set the focus to last item whenever data set is changed. 要根据需要显示最新事件,您需要设置android:transcriptMode="normal" ,默认情况下,每当更改数据集时,这会将焦点设置为最后一项。

http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:transcriptMode http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:transcriptMode

Also I you want to freeze scrolling then you can set setOnScrollListener() and update the list only then. 另外,我想冻结滚动,然后可以设置setOnScrollListener() ,然后仅更新列表。

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

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