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 ?
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.
For Kotlin is more easy just
myList.reverse()
do like this:
Collections.reverse(YourList); // ADD THIS LINE TO REVERSE ORDER!
YourList.notifyDataSetChanged;
I fixed this by using the same code as you included but I update the index variable by the number of items I add to the adapter before restoring the position. Seems to do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With