0

I have an app that reads data from realtime firebase database, when new data is posted it is displayed at the bottom of all other data, but this is not what I want, I want new data to be displayed at the top followed by old ones depending on their time and date they sent. This data is displayed in RecyclerView.

Here is my .xml file:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:listitem="@layout/item" />

And here the code that I have in onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_read);

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    final RecyclerView recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    productsList = new ArrayList<>();
    productAdapter = new Adapter(this, productsList);
    recyclerView.setAdapter(productAdapter);


}

How can I solve my problem. New data at the top and the oldest one at the bottom in that order

Zoe
  • 27,060
  • 21
  • 118
  • 148
tinoe
  • 216
  • 2
  • 11

1 Answers1

0

You can achieve the same by

LinearLayoutManager mLayoutManager = new LinearLayoutManager(MainActivity.this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
// And now set it to the RecyclerView
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(yourAdapter);
Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23