I have a mChildEventListener
that queries for the first n items from Firebase Database
and another mChildEventListener2
(triggered when the user scroll up and clicks on a Load More
button) that queries for the next n items and adds them to the ChatMessagesList
.
When the user clicks on the Load More
button: the ChatMessagesList
is updated and notifyDataSetChanged
is called. The mMessageListView
is updated but it scrolls down to the buttom every time. I explained this behavior because of the xml attribute: android:transcriptMode="alwaysScroll"
.
When I remove transcriptMode="alwaysScroll"
and the user presses on the Load More
button, the mMessageListView
scrolls this time to the top.
None of these behaviors are wanted. I would like that when the user presses the button, the mMessageListView
would maintain its' scrolling position. (for obvious reasons)
I tried calling notifyDataSetChanged
from a new thread. No results.
I tried using listView.setSelectionFromTop(lastViewedPosition, topOffset);
or using list.smoothScrollToPosition();
but none achieved a smooth experience that I would want.
How can I maintain the scrolling position after the mMessageListView
is updated?
This is the code:
private ChildEventListener mChildEventListener;
private ChildEventListener mChildEventListener2;
List<ChatMessage> ChatMessages = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mMessageAdapter = new MessageAdapter(this, R.layout.item_message_sent, ChatMessages);
mMessageListView.setAdapter(mMessageAdapter);
mChildEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(i == 0) {
current_last_key = dataSnapshot.getKey();
}
else {
ChatMessage chatMessage = dataSnapshot.getValue(ChatMessage.class);
mMessageAdapter.add(chatMessage);
}
i++;
}
...
};
Query messagesQuery = mDatabaseReference.child("chat_messages").
child(chatId).limitToLast(25);
messagesQuery.addChildEventListener(mChildEventListener);
...
}
@Override
public void onClick(View v) {
j = 0;
mChildEventListener2 = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(!dataSnapshot.getKey().equals(current_last_key)) {
if (j == 0) {
current_last_key = dataSnapshot.getKey();
}
else {
ChatMessage chatMessage = dataSnapshot.getValue(ChatMessage.class);
ChatMessages.add(j- 1, chatMessage);
}
}
else
{
ChatMessage chatMessage = dataSnapshot.getValue(ChatMessage.class);
ChatMessages.add(0 , chatMessage);
loadMoreTextView.setVisibility(View.GONE);
}
mMessageAdapter.notifyDataSetChanged();
j++;
}
... };
Query query = mDatabaseReference.child("chat_messages").
child(chatId).orderByKey().endAt(current_last_key).limitToLast(10);
query.addChildEventListener(mChildEventListener2);
}