使用 ItemTouchHelper.Callback 后如何按顺序设置项目位置

How to set Item position in order after using ItemTouchHelper.Callback

我正在用 RecyclerView 创建文档排列 activity。我想通过拖放来排列文档。使用ItemTouchHelper.Callback完成后OnItemMove回调后无法设置页码怎么办?

编辑:添加了代码片段

package adapters;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.pdf.PdfRenderer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.mobilix.docscanner.R;

import java.util.ArrayList;
import java.util.Collections;

import helper.ItemTouchHelperAdapter;
import helper.ItemTouchHelperViewHolder;
import helper.OnStartDragListener;
import helper.SimpleItemTouchHelperCallback;

public class PageAjdustAdapter extends RecyclerView.Adapter<PageAjdustAdapter.PageAdjustHolder> implements ItemTouchHelperAdapter, OnStartDragListener {
    private final String TAG = getClass().getName();
    Context mContext;
    ArrayList<PdfPage> pdfPages = new ArrayList<>();
    private ItemTouchHelper itemTouchHelper;

    public PageAjdustAdapter(Context context, ArrayList<PdfPage> pages) {
        this.mContext = context;
        pdfPages = pages;


        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(this);
        itemTouchHelper = new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(((Activity) context).findViewById(R.id.rcvPageArrange));

    }

    @NonNull
    @Override
    public PageAdjustHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_page_adajustment, parent, false);
        return new PageAdjustHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PageAdjustHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder: ");
        holder.ivPage.setImageBitmap(pdfPages.get(position).bitmap);
        holder.cbPage.setChecked(pdfPages.get(position).isSelected);
        holder.tvPageNo.setText(String.valueOf(position + 1));

        holder.cbPage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                pdfPages.get(holder.getAdapterPosition()).isSelected = isChecked;
            }
        });

        holder.ivRotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getAdapterPosition();
                pdfPages.get(pos).bitmap = Bitmap.createBitmap(//
                        pdfPages.get(pos).bitmap, 0, 0, pdfPages.get(pos).bitmap.getWidth(),//
                        pdfPages.get(pos).bitmap.getHeight(), pdfPages.get(pos).matrix, true);//

                notifyItemChanged(pos);
            }
        });
    }

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

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        Log.d(TAG, "onItemMove: ->fp " + (fromPosition + 1) + " tp-> " + (toPosition + 1));
        Collections.swap(pdfPages, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);

        
        return false;
    }

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

    @Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
        itemTouchHelper.startDrag(viewHolder);

    }


    public static class PageAdjustHolder extends RecyclerView.ViewHolder implements
            ItemTouchHelperViewHolder {
        ImageView ivPage, ivRotate;
        CheckBox cbPage;
        TextView tvPageNo;

        public PageAdjustHolder(@NonNull View itemView) {
            super(itemView);
            ivPage = itemView.findViewById(R.id.ivPage);
            ivRotate = itemView.findViewById(R.id.ivRotate);
            cbPage = itemView.findViewById(R.id.cbPage);
            tvPageNo = itemView.findViewById(R.id.tvPageNo);
        }

        @Override
        public void onItemSelected() {

        }

        @Override
        public void onItemClear() {

        }
    }

    public static class PdfPage {
        PdfRenderer.Page page;
        Bitmap bitmap;
        boolean isSelected;
        Matrix matrix;
        int rotate = 0;

        public PdfPage(PdfRenderer.Page page, Bitmap bitmap) {
            this.page = page;
            this.bitmap = bitmap;
            matrix = new Matrix();
            rotate += 90;
            matrix.postRotate(90);//martix work on +=90
        }
    }

}

您必须在更改项目位置时调用 notifyDataSetChanged()。最简单的方法是使用 onItemClear(),它会在项目为 de-selected 时调用。像下面这样添加。

    @Override
    public void onItemClear() {
          notifyDataSetChanged();
    }

你必须添加的一件事是检查位置是否在拖动操作后实际改变,否则无论何时选择一个项目,它总是会更新整个数据集,而不是 de-selected。

编辑

在 view-holder class 中创建一个局部变量。比你只需要在 onItemSelected() 中设置它并在 onItemClear();

中检查它
    @Override
    public void onItemSelected() {
        lastpos = getAdapterPosition();
    }


    @Override
    public void onItemClear() {
         if(lastpos != getAdapterPosition())
              notifyDataSetChanged();
    }