Item交换后跟新数据

package cn.xiaozhibo.com.app.adapter;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;
import java.util.List;

import cn.xiaozhibo.com.kit.bean.EditSortData;

/**
 * @author writing
 */
public class EditSortTouchHelperCallBack extends ItemTouchHelper.Callback {
    private List mEditSortDataList;
    private EditSortAdapter editSortAdapter;

    public EditSortTouchHelperCallBack(List mEditSortDataList, EditSortAdapter editSortAdapter) {
        this.mEditSortDataList = mEditSortDataList;
        this.editSortAdapter = editSortAdapter;
    }

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN|ItemTouchHelper.START|ItemTouchHelper.END;
        return makeMovementFlags(dragFlags,0);
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        int fromPosition = viewHolder.getBindingAdapterPosition();
        int toPosition = target.getBindingAdapterPosition();
        Collections.swap(mEditSortDataList,fromPosition,toPosition);
        editSortAdapter.notifyItemMoved(fromPosition,toPosition);
        //注
        editSortAdapter.notifyItemRangeChanged(Math.min(fromPosition, toPosition), Math.abs(fromPosition - toPosition) +1);

        return true;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

    }
}

我们的需求是交换Item,但是第0个位置的Item和第8个的Item不一样,交换后( editSortAdapter.notifyItemMoved(fromPosition,toPosition);)还需要调用下面的代码

 editSortAdapter.notifyItemRangeChanged(Math.min(fromPosition, toPosition), Math.abs(fromPosition - toPosition) +1);

Item交换后跟新数据_第1张图片

你可能感兴趣的:(随笔,android)