Android RecyclerView将项目移动到View的结尾

时间:2016-04-24 14:14:07

标签: android animation android-recyclerview adapter

我在Android RecyclerView上显示商品,我希望能够选择任何商品,并将其放在列表的最后。我知道如何添加或删除项目,但我正在寻找最简洁的方法来实现这一目标。

如果可能的话,我也在寻找一个能让这个项目在其他地方飞越的动画。

这是我的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);
    }
}

2 个答案:

答案 0 :(得分:1)

我不知道notifyItemMoved()功能存在。 我只是从我的arraylist中删除了该元素,并在最后将其添加回来。然后我打电话给notifyItemMoved()。使用ItemAnimator,我得到了我想要的动画。

答案 1 :(得分:0)

将项目移至List的末尾,然后重新加载Adapter

编辑: 要重新加载Adapter使用:

adapter.notifyDataSetChanged()