检测一个ListView内滚动位置(或滚动型)位置、ListView

2023-09-06 07:02:06 作者:拥一生入怀

我要建在这里的新事件正在调查每一个X秒一个聊天室的应用程序。出现这种情况时,都会这样code更新RoomAdapter(ArrayAdapter的自定义子类)的新数据和滚动它的底部:

I'm building a chat room application where new events are being polled for every X seconds. Every time that happens, this code updates the RoomAdapter (a custom subclass of ArrayAdapter) with the new data and scrolls it to the bottom:

RoomAdapter adapter = (RoomAdapter) getListAdapter();
for (int i=0; i<newEvents.size(); i++)
 adapter.add(newEvents.get(i));
getListView().setSelection(events.size()-1);

这是好的,但如果用户向上滚动浏览一下房间的历史,它会跳到她下到谷底时,投票情况。我想要做的是有它这样,如果用户已经在列表的底部,它的一项民意调查新的事件发生后在那里停留。如果用户的故意向上滚动,我想不被人打扰用户。

This is fine, except that if the user is scrolling up to glance at the room's history, it's going to jump her down to the bottom when the poll happens. What I'd like to do is have it so that if the user is already at the bottom of the list, it stays there after a poll for new events. If the user's intentionally scrolled upwards, I'd like the user not to be disturbed.

如何检测的时候我已经滚动到ListView底部?

How do I detect when I'm already scrolled to the bottom of a ListView?

注:我不能做getListView()getSelectedItemPosition(),因为的作为文档注意,调用setSelection当触摸模式实际上不是选择项目,只需滚动到它。

Note: I can't do getListView().getSelectedItemPosition(), because as the docs note, calling setSelection when in touch mode does not actually select the item, just scrolls to it.

推荐答案

我还没有关于想这一点,但如何getLastVisiblePosition()== getCount将() - 1 ? (在方法的ListView

I haven't tried this, but how about getLastVisiblePosition()==getCount()-1? (methods on ListView)