简体   繁体   English

由于recyclerview拖放的变化,如何更新ArrayList中的所有项目

[英]How to update all the items in ArrayList due to shift in drag and drop of recyclerview

I have a RecyclerView whose layout is a GridLayout of 3 columns. 我有一个RecyclerView其布局是3列的GridLayout I am using ItemTouchHelper to drag and drop in the GridLayout of the RecyclerView . 我正在使用ItemTouchHelper拖放RecyclerViewGridLayout In my Adapter there is a method onItemMove() which notify me the items which i have moved. 在我的适配器中,有一个方法onItemMove()通知我我已移动的项目。 It only swaps those items in the ArrayList which I have dragged and dropped. 它只交换我拖放的ArrayList那些项。 But due to the drag and drop there is a shift in other elements as well. 但是由于拖放,其他元素也会发生变化。 I want to update their positions as well in the ArrayList . 我也想更新它们在ArrayList的位置。

Here is the Adapter of my Recyclerview: 这是我的Recyclerview的适配器:

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.MyViewHolder> implements com.sagar.quizdemo.helper.ItemTouchHelperAdapter {
    String[] str = {"Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6", "Level 7", "Level 8", "Level 9"};
    List<String> itemList, actualList;
    LayoutInflater inflater;
    Context context;
    private final OnStartDragListener mDragStartListener;

    public GridAdapter(Context context, OnStartDragListener dragStartListener) {
        this.context = context;
        mDragStartListener = dragStartListener;
        inflater = LayoutInflater.from(context);
        itemList = new ArrayList<>();
        actualList = new ArrayList<>(Arrays.asList(str));
    }

    public void getItemList(List<String> nameList) {
        int currentSize = itemList.size();
        itemList.clear();
        itemList.addAll(nameList);
        notifyItemRangeRemoved(0, currentSize);
        notifyItemRangeInserted(0, nameList.size());
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.custom_level_row, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.textView.setText(itemList.get(position));
        // Start a drag whenever the handle view it touched
        holder.cardView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                    mDragStartListener.onStartDrag(holder);
                }
                return false;
            }
        });
    }

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

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        Collections.swap(itemList, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        notifyItemChanged(fromPosition);
        notifyItemChanged(toPosition);
        return true;
    }

    @Override
    public void onItemDismiss(int position) {
        itemList.remove(position);
        notifyItemRemoved(position);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder {

        CardView cardView;
        TextView textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.gameCard);
            textView = (TextView) itemView.findViewById(R.id.gameText);
        }

        @Override
        public void onItemSelected() {
            itemView.setBackgroundColor(Color.LTGRAY);
        }

        @Override
        public void onItemClear() {
            itemView.setBackgroundColor(Color.WHITE);
        }
    }
}

if initially the list is : 如果最初的列表是:

{"Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6", "Level 7", "Level 8", "Level 9"}

After I swap 2nd and 4th positioned elements in the RecyclerView. 之后,我在RecyclerView中交换了第二和第四定位的元素。 The updated list should look like this ( which I want ): 更新后的列表应如下所示( 我想要 ):

{"Level 1", "Level 5", "Level 2", "Level 3", "Level 4", "Level 6", "Level 7", "Level 8", "Level 9"}

But I am getting this updated list: 但是我得到这个更新的列表:

{"Level 1", "Level 5", "Level 3", "Level 4", "Level 2", "Level 6", "Level 7", "Level 8", "Level 9"}

I believe that your problem is that you use Collections.swap(itemList, fromPosition, toPosition); 我相信您的问题是您使用了Collections.swap(itemList, fromPosition, toPosition); , which swaps objects on the specified positions. ,该对象交换指定位置上的对象。 So the output is correct. 所以输出是正确的。 What you need, though: 不过,您需要什么:

String item = itemList.remove(fromPostion);
itemList.insert(item, toPosition);

Try calling notifyDataSetChanged() in onItemMove menthod. 尝试在onItemMove方法中调用notifyDataSetChanged()。

@Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        notifyItemMoved(fromPosition, toPosition);
        notifyItemChanged(fromPosition);
        notifyItemChanged(toPosition);
        notifyDataSetChanged();    
        return true;
    }

Try this instead. 试试这个吧。 In your case, you will first have to find the index of the item to be replaced (fromPosition) and then add toPosition to that index. 对于您的情况,您首先必须找到要替换的项目的索引(fromPosition),然后将toPosition添加到该索引。 How to move specific item in array list to the first item 如何将数组列表中的特定项目移动到第一项

Code here working for me. 代码在这里为我工作。

 @Override
public boolean onItemMove(int fromPosition, int toPosition) {
    if (fromPosition < toPosition) {
        for (int i = fromPosition; i < toPosition; i++) {
            Collections.swap(listData, i, i + 1);
        }
    } else {
        for (int i = fromPosition; i > toPosition; i--) {
            Collections.swap(listData, i, i - 1);
        }
    }
    notifyItemMoved(fromPosition, toPosition);
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM