前言
之前用csdn上的网易频道管理的demo,代码量太大。最近自己用recycleview修改一下,总共使用了六个类,不会超过600行的代码。和大家交流。
/**
* 处理RecycleView的选中,拖拽移动,拖拽删除的实现类
*/
public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
private ItemTouchHelperAdapter mAdapter;
public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
mAdapter = adapter;
}
@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 isLongPressDragEnabled() {
return false;//长按启用拖拽
}
@Override
public boolean isItemViewSwipeEnabled() {
return false; //不启用拖拽删除
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
//通过接口传递拖拽交换数据的起始位置和目标位置的ViewHolder
mAdapter.onItemMove(source, target);
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
//移动删除回调,如果不用可以不用理
// mAdapter.onItemDissmiss(viewHolder);
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
//当滑动或者拖拽view的时候通过接口返回该ViewHolder
mAdapter.onItemSelect(viewHolder);
}
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
if (!recyclerView.isComputingLayout()) {
//当需要清除之前在onSelectedChanged或者onChildDraw,onChildDrawOver设置的状态或者动画时通过接口返回该ViewHolder
mAdapter.onItemClear(viewHolder);
}
}
}
由于其他频道和我的频道中的1和 2不能拖拽,所以SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback 中的isLongPressDragEnabled(),禁止拖拽,重写点击事件和长按点击事件,调用 touchHelper.startDrag(childViewViewHolder)来执行拖拽
public boolean isLongPressDragEnabled() {
return false;//长按启用拖拽
}
在下面的RecyclerItemClickListener 用来重写点击事件和长按点击事件
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener{
private View childView;
private RecyclerView touchView;
private RecyclerView.ViewHolder viewHolder;
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final OnItemClickListener mListener) {
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent ev) {
if (childView != null && mListener != null) {
mListener.onItemClick(childView, touchView.getChildPosition(childView),viewHolder);
}
return true;
}
@Override
public void onLongPress(MotionEvent ev) {
if (childView != null && mListener != null) {
mListener.onLongClick(childView, touchView.getChildPosition(childView),viewHolder);
}
}
});
}
public interface OnItemClickListener {
public void onItemClick(View view, int position, RecyclerView.ViewHolder viewHolder);
public void onLongClick(View view, int posotion, RecyclerView.ViewHolder viewHolder);
}
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
mGestureDetector.onTouchEvent(motionEvent);
childView = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
touchView = recyclerView;
if (childView!=null){
viewHolder = recyclerView.getChildViewHolder(childView);
}
return false;
}
@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
欢迎大家下载,留言 star fork
下载地址
http://download.csdn.net/detail/bu_wangchuxin/9843213
github 下载地址
https://github.com/fangyangdexingxing999/DragChannelManager