gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:22:58 26 4
gpt4 key购买 nike

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

如果可能的话,我也在寻找一个动画,显示这个项目在下降时飞过其他项目。

这是我的适配器:

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);
}
}

最佳答案

我不知道 notifyItemMoved() 函数存在。我只是从我的 arraylist 中删除了元素,然后在末尾添加回来。然后我调用了 notifyItemMoved()。使用 ItemAnimator,我得到了我想要的动画。

关于Android RecyclerView 将项目移动到 View 的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36824226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com