Hammad Nasir Hammad Nasir - 5 months ago 112
Android Question

How to add newly added data from firebase to the top of the recyclerview and not at the bottom? Please see details

I'm developing an app in which I'm retrieving some data from

Firebase
database.

The problem is that everytime I save something, it gets retrieved and gets added to the bottom of the
RecyclerView
.

What I want is I want the newly added data to get added to the top of the recyclerview and not at the bottom.

I have no idea how to do what I want to do.

Please let me know.

Answer

well first of all the question is about recyclerview and you posted code of your data retrieval. and if your want to add a new item to recycler view use

mArrayList.add(position, item);
notifyItemInserted(position); 

for data to be inserted in first row use

mArrayList.add(0, item);
notifyItemInserted(0); 

and then you can use mRecyclerView.smoothScrollToPosition(0); to scroll to the top.