老规矩,先上效果图
拖拽排序固定首位.gif
上代码--两个类
IDragSwipe回调类
DragSwipeCallback拖拽功能类
默认是上下拖拽排序,左右滑动删除,根据自己公司业务去修改getMovementFlags()
/**
* @Author: CC
* @CreateDate: 2020/8/31 17:14
* @Description: recycleView拖拽排序类
*/
public class DragSwipeCallback extends ItemTouchHelper.Callback {
/**
* 通过此变量通知外界发生了排序、删除等操作
*/
private IDragSwipe mDragSwipe;
private Boolean mLongPressDragEnabled = false;//是否长按移动
private Boolean mItemViewSwipeEnabled = false;//是否滑动删除
private int mFixedPosition = -1; //固定的下标(不可移动)
/**
* 固定某行不可以拖动
*
* @param fixedPosition
*/
public void setFixedPosition(int fixedPosition) {
mFixedPosition = fixedPosition;
}
public DragSwipeCallback(IDragSwipe dragSwipe) {
mDragSwipe = dragSwipe;
}
public DragSwipeCallback(IDragSwipe dragSwipe, Boolean isLongPressDragEnabled, Boolean isItemViewSwipeEnabled) {
mDragSwipe = dragSwipe;
mLongPressDragEnabled = isLongPressDragEnabled;
mItemViewSwipeEnabled = isItemViewSwipeEnabled;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
// 确定拖拽、滑动支持的方向
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean isLongPressDragEnabled() {
//是否开启拖拽排序
return mLongPressDragEnabled;
}
@Override
public boolean isItemViewSwipeEnabled() {
//是否开启滑动删除
return mItemViewSwipeEnabled;
}
/**
* 拖拽、交换事件
*/
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
if (viewHolder.getAdapterPosition() == mFixedPosition || target.getAdapterPosition() == mFixedPosition) {
return false;
}
mDragSwipe.onItemSwapped(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
/**
* 滑动成功的事件
*/
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
switch (direction) {
case ItemTouchHelper.END: // START->END 标记完成事件
mDragSwipe.onItemDone(viewHolder.getAdapterPosition());
break;
case ItemTouchHelper.START: // END->START 删除事件
mDragSwipe.onItemDeleted(viewHolder.getAdapterPosition());
break;
default:
}
}
/**
* 拖拽、滑动时如何绘制列表
* actionState只会为ACTION_STATE_DRAG或者ACTION_STATE_SWIPE
*/
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (viewHolder.getAdapterPosition() == mFixedPosition) {
return;
}
View rootView = viewHolder.itemView;
switch (actionState) {
case ItemTouchHelper.ACTION_STATE_DRAG:
// 拖拽时排序,如果是isCurrentlyActive,则设置translationZ,否则复位
// viewHolder.itemView.setTranslationZ(ResourceUtils.dp2px(isCurrentlyActive ? 4 : 0));
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
rootView.setBackgroundResource(isCurrentlyActive ? R.color.default_ripple_bg : R.color.lib_no_color);
break;
case ItemTouchHelper.ACTION_STATE_SWIPE:
// 滑动删除时,对view的绘制
rootView.setBackgroundResource(R.color.online_tag_text_color);
rootView.setTranslationX(dX);
break;
default:
break;
}
}
/**
* 在onSelectedChanged、onChildDraw、onChildDrawOver操作完成后可以在此进行清楚操作
*/
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
View rootView = viewHolder.itemView;
rootView.setTranslationX(0);
}
}
/**
* @Author: cc
* @CreateDate: 2020/8/31 17:13
* @Description:item长按删除拖动移动的回调
*/
public interface IDragSwipe {
/**
* 两个Item交换位置
*
* @param fromPosition 第一个Item的位置
* @param toPosition 第二个Item的位置
*/
void onItemSwapped(int fromPosition, int toPosition);
/**
* 删除Item
*
* @param position 待删除Item的位置
*/
default void onItemDeleted(int position) {
}
/**
* Item标记完成
*
* @param position Item的位置
*/
default void onItemDone(int position) {
}
}
使用方法
在页面实现IDragSwipe方法
/**
* 拖拽排序的回调
*
* @param fromPosition 第一个Item的位置
* @param toPosition 第二个Item的位置
*/
@Override
public void onItemSwapped(int fromPosition, int toPosition) {
List dataList = mWrapper.getDataList();
if (dataList == null || dataList.size() == 0) {
return;
}
if (toPosition == dataList.size()) {
return;
}
//排序
Collections.swap(dataList, fromPosition, toPosition);
mWrapper.notifyItemMoved(fromPosition, toPosition);
}
设置回调和监听
ItemTouchHelper.Callback callback = new DragSwipeCallback(this::onItemSwapped, true, false);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(mRecyclerView);
网友评论