Add items to the top of Recyclerview with Firebaserecycleradapter

Asked

Viewed 327 times

0

How do I add items to the top of a RecyclerView using the FirebaseRecyclerAdapter?

By default, whenever I add an item, it goes to the bottom of the Recyclerview list, and I want it to go to the top. Remembering that this Adapter is different from the standard of Android.

noteAdapter = new FirebaseRecyclerAdapter<Note, NoteHolder>(Note.class, R.layout.note_item, NoteHolder.class, dbRef) {

            @Override
            protected void populateViewHolder(NoteHolder viewHolder, final Note model, final int position) {
                viewHolder.setTitle(model.getTitle());
                viewHolder.setContent(model.getContent());
                viewHolder.setColor(model.getColorBg());

                model.setId(getRef(position).getKey());

                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent it = new Intent(v.getContext(), NoteDetailActivity.class);
                        it.putExtra(Note.NOTE_PARCE, model);
                        startActivity(it);
                    }
                });
            }
        };

        notesRecyclerView.setLayoutManager(layoutManager);
        notesRecyclerView.setAdapter(noteAdapter);

UPDATE: Researching on, I saw that this adapter picks up the information exactly as it is on firebase, including the order, so I would have to insert already in the right order in the firebase, but how to do this?

1 answer

1

You can use the static method reverse() class Collections. Since your code does not contain where you rescue the list items, I will give a basic example below:

    ArrayList<String> actores = new ArrayList<String> ();
    actores.add("JON SNOW");
    actores.add("DAENERYS");
    actores.add("KHAL DROGO");
    actores.add("NED STARK");
    actores.add("MONTANHA ");
    actores.add("TYWIN LANNISTER");

    System.out.print(actores);

Exit:

[JON SNOW, DAENERYS, KHAL DROGO, NED STARK, MONTANHA , TYWIN LANNISTER]

Now using Collections.reverse():

Collections.reverse(actores);
System.out.print(actores);

Exit:

[TYWIN LANNISTER, MONTANHA , NED STARK, KHAL DROGO, DAENERYS, JON SNOW]

Note that you have reversed the list in relation to the insertion. So, just adapt to your needs. For more details see in the documentation.

Behold functioning on the ideone.

  • I’m not using list, this Adapter already does everything, it returns the object I want, but I don’t keep it in a list, and you don’t use list on it, that’s the problem, it does everything... But I need to use it for recyclerview

  • @Felipe.rce What is this dbRef then?

  • Databasereference variable, it’s not a list, it’s firebase stuff... I even tried to use list to manipulate the order, but not da, firebase puts that same, only solution is to insert in firebase... Insert in the right order, but I’ve researched a lot and I haven’t seen anything about

Browser other questions tagged

You are not signed in. Login or sign up in order to post.