Detecting scroll position inside ListView (or ScrollView)

I am creating a chat application in which new events are polled every X seconds. Each time this happens, this code updates the RoomAdapter (a custom subclass of ArrayAdapter) with the new data and scrolls it below:

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

This is great, except that if the user scrolls to look at the history of the room, he is about to jump down when the poll occurs. I would like to make sure that if the user is already at the bottom of the list, he will remain there after polling for new events. If the user intentionally scrolls up, I would like the user not to worry.

How to determine when I am already scrolling to the bottom of a ListView?

Note: I cannot do getListView (). getSelectedItemPosition (), because as a note to documents , calling setSelection when touched does not actually select the element, it simply scrolls it.

+2
source share
2 answers

I have not tried this, but what about getLastVisiblePosition()==getCount()-1 ? (methods on ListView )

+6
source

But it's not right.
Use
android:transcriptMode
- List behavior when changing content
- "disabled", does not scroll
- "normal", scrolls down if the last element is visible
- "alwaysScroll", always scrolls to the bottom
android:stackFromBottom
- Stack items in reverse order
- starts with the last item from the adapter
• Useful for chats
- Messages, Discussion, IRC, etc.

+5
source

All Articles