RecyclerView拖动排序索引错误

时间:2018-09-13 12:15:26

标签: android android-recyclerview itemtouchhelper

我使用ItemTouchHelper实现RecyclerView拖动排序。问题在于ViewHolder已更改,但ViewHolder的索引未更改。怎么了?谢谢!!!

ItemTouchHelperCallback:

   @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        if (viewHolder.getItemViewType() != target.getItemViewType()) {
            return false;
        }
        adapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

适配器:

@Override
public void onItemMove(int fromPosition, int toPosition) {
    Collections.swap(mData, fromPosition, toPosition);
    notifyItemMoved(fromPosition, toPosition);
}

2 个答案:

答案 0 :(得分:0)

希望它对您有用

SwipeAndDragHelper swipeAndDragHelper = new SwipeAndDragHelper(adapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(swipeAndDragHelper);
adapter.setTouchHelper(touchHelper);
recyclerview.setAdapter(adapter);
touchHelper.attachToRecyclerView(recyclerview);

// SwipeAndDragHelper类

public class SwipeAndDragHelper extends ItemTouchHelper.Callback {
private ActionCompletionContract contract;

public SwipeAndDragHelper(ActionCompletionContract contract) {
    this.contract = contract;
}

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {

    int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
    int swipeFlags = 0;
    return makeMovementFlags(dragFlags, swipeFlags);
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    contract.onViewMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
    return true;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
 //   contract.onViewSwiped(viewHolder.getAdapterPosition());
}

@Override
public boolean isLongPressDragEnabled() {
    return false;
}

@Override
public void onChildDraw(Canvas c,
                        RecyclerView recyclerView,
                        RecyclerView.ViewHolder viewHolder,
                        float dX,
                        float dY,
                        int actionState,
                        boolean isCurrentlyActive) {
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        float alpha = 1 - (Math.abs(dX) / recyclerView.getWidth());
        viewHolder.itemView.setAlpha(alpha);
    }
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}

public interface ActionCompletionContract {
    void onViewMoved(int oldPosition, int newPosition);

    void onViewSwiped(int position);
}

}

//呼叫触摸方法

holder.imageview.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                        touchHelper.startDrag(holder);
                    }
                    return false;
                }
            });


public void setTouchHelper(ItemTouchHelper touchHelper) {
    this.touchHelper=touchHelper;
}

//实施方法

public class CustomDetail extends RecyclerView.Adapter<CustomDetail.ViewHolder>    implements
    SwipeAndDragHelper.ActionCompletionContract {




@Override
public void onViewMoved(int oldPosition, int newPosition) {
    FavouriteListResponse.Data listdat=listview.get(oldPosition);
    listview.remove(oldPosition);
    listview.add(newPosition, listdat);
    notifyItemMoved(oldPosition, newPosition);

}

@Override
public void onViewSwiped(int position) {
    listview.remove(position);
    notifyItemRemoved(position);
}

答案 1 :(得分:0)

@pskink @Monika Arora非常感谢。当我将adapter.notifyDataSetChanged();添加到类clearView的{​​{1}}方法中时, ViewHolder 的位置正确。如下:

ItemTouchHelperCallback