Add items at the top of RecyclerView with FirebaseRecyclerAdapter

0

How do I add items to the top of a RecyclerView using 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 start. Remember that this adapter is different from the Android standard.

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: Searching, I saw that this adapter gets the information exactly as it is in firebase , including the order, so I would have to insert already in the right order in firebase , but how to do that?

    
asked by anonymous 10.06.2017 / 01:18

1 answer

1

You can use the static method reverse() of class Collections . Since your code does not contain where to retrieve the items from the list, I'll give you 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);

Output:

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

Now using Collections.reverse() :

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

Output:

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

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

See working on ideone .

    
10.06.2017 / 02:19