Android – FirebaseListAdapter in reverse order?

This is not the exact solution of your problem of getting the data from firebase in a reverse order, but anyway, we’ve other work arounds.

The first work around is mentioned in a comment of using a custom adapter for your list.

To achieve the behaviour you need to get the firebase data in a list and then you’ve to reverse it yourself before passing it to an adapter. Simple!

The second work around is easy as pie and I think if you’re using RecyclerView it could do the trick for you and I see it as the simplest way you can do the job.

Here’s I’m modifying some of my code with RecyclerView

// Declare the RecyclerView and the LinearLayoutManager first
private RecyclerView listView;
private LinearLayoutManager mLayoutManager;

public void onViewCreated(View view, Bundle savedInstanceState){

    // Use FirebaseRecyclerAdapter here

    // Here you modify your LinearLayoutManager
    mLayoutManager = new LinearLayoutManager(MainActivity.this);

    // Now set the layout manager and the adapter to the RecyclerView

By setting mLayoutManager.setReverseLayout(true); – you’re reversing your layout and mLayoutManager.setStackFromEnd(true); positions the view to the top of your list.

Migrating to RecyclerView is simple. Your layout will be something like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

        android:layout_height="match_parent" />

And in your build.gradle

dependencies {
    compile ''

You need to have FirebaseRecyclerAdapter can be found here in FirebaseUI library.

Note: Do not use RecyclerView.LayoutManager as the setReverseLayout and setStackFromEnd functions won’t be found in RecyclerView.LayoutManager. Use LinearLayoutManager as stated.


Here’s how you can handle the click events of your items in the list.

You had to declare a ViewHolder to implement the RecyclerView right? Just add another function inside your ViewHolder class like the example below and call this function after the setText functions you’ve got there.

public static class MyViewHolder extends RecyclerView.ViewHolder {
    View mView;

    public MyViewHolder(View itemView) {
        mView = itemView;

    public void setClickEvent() {
        // Set the onClickListener on mView
        // mView.setOnClickListener(new OnClickListener)...

Since you’re already extending FirebaseListAdapter to implement populateView, you can go ahead and override getItem as well to invert the item lookup:

public HashMap getItem(int pos) {
    return super.getItem(getCount() - 1 - pos);

If you’re not using RecyclerView this is a simple way to reverse the data without extending any more classes than necessary. Obviously this will reverse any AdapterView subclass backed by this adapter, if you choose to reuse it.

Leave a Comment