Basically, whenever I load data into my recyclerView it automatically scrolls to the bottom of the recyclerView
here what my function looks like :
mMessagesDBRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String key = getIntent().getStringExtra("key");
final String currentUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
if (dataSnapshot.child(key).child(currentUid).exists() && !dataSnapshot.child(currentUid).child(key).exists()) {
mMessagesDBRef.child(key).child(currentUid).limitToLast(TOTAL_ITEMS_TO_LOAD* mCurrentPage).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mMessagesList.clear();
for (DataSnapshot snap : dataSnapshot.getChildren()) {
ChatMessage chatMessage = snap.getValue(ChatMessage.class);
mMessagesList.add(chatMessage);
populateMessagesRecyclerView();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
private void populateMessagesRecyclerView() {
adapter = new messageAdapter(mMessagesList, this);
mChatsRecyclerView.setAdapter(adapter);
}
And this some of my xml file:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refreshLayout"
android:layout_below="@id/imageView11"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@id/imageView14">
<android.support.v7.widget.RecyclerView
android:background="#f9f9f9"
android:id="@+id/messagesRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
Is there any way I can deactivate the automatic scrolling. And thanks
In your for loop, move your populateMessagesRecyclerView()
, out of it
...
for (DataSnapshot snap : dataSnapshot.getChildren()) {
ChatMessage chatMessage = snap.getValue(ChatMessage.class);
mMessagesList.add(chatMessage);
}
populateMessagesRecyclerView();
}
...
If you let that method inside your for loop, it will create x instances of the adapter and will do the job many times, keeping it out, you are sure that you only will instantiate the adapter once with your list data.
If your data from Firebase is coming in the wrong order (assuming the old data is beign bring first instead of the new one)
To fix this , inside your populateMessagesRecyclerView();
you can reverse your RecyclerView
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
mChatsRecyclerView.setLayoutManager(linearLayoutManager);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.