RecyclerView scrolls to bottom whenever I load the data from Firebase

Joee :

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

Gastón Saillén :

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);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162248&siteId=1
Recommended