Florian Mac Langlade Florian Mac Langlade - 4 months ago 26
Android Question

Android - Reverse listview as message display

I've got a little question for my Android App.
I searched for a long time but found nothing about my problem.

Scenario : I've to display a revert listview (as Facebook Messenger).
And when the user scrolls to the top, load more messages.

Problem : After

notifiyDataAsChanged()
call, the scroll is not the same!
I want to conserve exactly the same position as before loading.

I tried that code :

// save index and top position
int index = list.getFirstVisiblePosition()+result.size();
View v = list.getChildAt(index);
int top = (v == null) ? 0 : v.getTop();

// ...
// restore
list.setSelectionFromTop(index, top);


But the scroll is not exactly the same after loading.

Have you got an idea ?

Answer Source

I think you should look into using TranscriptMode and StackFromBottom with your listview:

android:stackFromBottom="true"
android:transcriptMode="normal"

By setting the transcript mode to normal it will scroll to the bottom only if the last item was already in view. That way your users can view the previous list view items without interruption when you call notifydatasetchanged.