1

I am displaying Items on a Android RecyclerView, and I would like to be able to select any item, and place it at then very end of the list. I know how to Add or Remove items, but I am looking for the cleanest way to achieve this.

If possible, I am also looking for an animation that would show this item flying over the others, when going down.

Here is my adapter :

public class DiscoverViewAdapter extends RecyclerView.Adapter<DiscoverViewAdapter.ViewHolder> {

    private List<DiscoveredUser> _items;

    public DiscoverViewAdapter(List<DiscoveredUser> items) {
        this._items = items;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        TextView userName;
        TextView userAge;
        TextView userDescription;

        public ViewHolder(View itemView)
        {
            super(itemView);
            userName = (TextView) itemView.findViewById(R.id.user_name);
            userAge = (TextView) itemView.findViewById(R.id.user_age);
            userDescription = (TextView) itemView.findViewById(R.id.user_description);
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.user_discover_row, viewGroup, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        DiscoveredUser user = _items.get(i);

        viewHolder.userName.setText(user.getUsername());
        viewHolder.userAge.setText(Integer.toString(user.getAge()));
        viewHolder.userDescription.setText(user.getDescription());
    }

    @Override
    public int getItemCount() {
        return _items.size();
    }

    public void remove(int position) {
        //_items.remove(position);
        //notifyItemRemoved(position);
    }

    public void add(DiscoveredUser user, int position) {
        _items.add(position, user);
        notifyItemInserted(position);
    }

    public void addMultiple(DiscoveredUser[] users){

        final int size = users.length;
        for (DiscoveredUser user : users) {
            _items.add(0, user);
        }
        notifyItemRangeInserted(0, size);
    }
}
Scaraux
  • 3,841
  • 4
  • 40
  • 80

2 Answers2

3

I didn't know that the notifyItemMoved() function existed. I simply removed the element from my arraylist, and added it back at the end. Then I called notifyItemMoved(). Using an ItemAnimator, I got the animation I wanted.

Scaraux
  • 3,841
  • 4
  • 40
  • 80
0

Move the Item to the end of your List and reload your Adapter.

Edit: To reload the Adapter use:

adapter.notifyDataSetChanged()
Jonas Franz
  • 555
  • 1
  • 8
  • 18
  • What method should I use to reload the RecyclerView ? One of those ? `notifyItemChanged(int) notifyItemInserted(int) notifyItemRemoved(int) notifyItemRangeChanged(int, int) notifyItemRangeInserted(int, int) notifyItemRangeRemoved(int, int)` – Scaraux Apr 24 '16 at 14:53
  • I am not using a listview, the way of updating and notifying a `RecyclerView`differs from a simple `Listview` – Scaraux Apr 24 '16 at 14:57
  • But you are using Adapter and every adapter has a notifyDataSetChanged method. – Jonas Franz Apr 24 '16 at 14:58
  • Yep but this method is supposed to be called when the dataSet has been replaced, not when data has been modified in the set. That is the difference with listviews – Scaraux Apr 24 '16 at 15:05