RecyclerView拖拽排序item工具类
package com.baselib.utils;
import com.baselib.base.adapter.BaseRVAdapter;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
public class RVItemTouchHelper<T> extends ItemTouchHelper.Callback {
private List<T> data;
private BaseRVAdapter adapter;
private boolean enabled=false;
public RVItemTouchHelper(List<T> data, BaseRVAdapter commonFunctionsAdapter) {
this.data = data;
this.adapter = commonFunctionsAdapter;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
return makeMovementFlags(dragFlags, 0);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition();
int toPosition = target.getAdapterPosition();
if (fromPosition < data.size() && toPosition < data.size()) {
T item = data.get(fromPosition);
data.remove(fromPosition);
data.add(toPosition, item);
adapter.notifyItemMoved(fromPosition, toPosition);
}
return true;
}
/**
* Item是否可以被滑动(H:左右滑动,V:上下滑动)
* isItemViewSwipeEnabled()返回值是否可以拖拽排序,true可以,false不可以
* @return true
*/
@Override
public boolean isItemViewSwipeEnabled() {
//return super.isItemViewSwipeEnabled();
return enabled;
}
/**
* 当Item被长按的时候是否可以被拖拽
*
* @return true
*/
@Override
public boolean isLongPressDragEnabled() {
//return super.isLongPressDragEnabled();
return enabled;
}
public void setItemSwipeEnabled(boolean enabled){
this.enabled=enabled;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
viewHolder.itemView.setScaleX(1f);
viewHolder.itemView.setScaleY(1f);
}
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setScaleX(1.0f);
viewHolder.itemView.setScaleY(1.0f);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState ==RecyclerView.SCROLL_STATE_IDLE){
//暂停
adapter.notifyDataSetChanged();//recyclerView滑动中不能调用此方法,所以得监听滑动
if (listener!=null){
listener.moveListener(data);
}
}
}
});
}
private OnItemMoveListener listener;
public void setOnItemMoveListener(OnItemMoveListener listener){
this.listener=listener;
}
public interface OnItemMoveListener{
<T> void moveListener(T param);
}
}
使用
RVItemTouchHelper cardHelperCallBack= new RVItemTouchHelper(topics, listAdapter);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(cardHelperCallBack);
itemTouchHelper.attachToRecyclerView(rvTopicList);
cardHelperCallBack.setItemSwipeEnabled(true);
cardHelperCallBack.setOnItemMoveListener(new RVItemTouchHelper.OnItemMoveListener() {
@Override
public <T> void moveListener(T param) {//保存按钮显示
if (tvSaveSort.getVisibility()==View.GONE){
tvSaveSort.setVisibility(View.VISIBLE);
}
listAdapter.notifyItemAdapters();
}
});
网友评论