从ListView开始,就有许多开源的滑动删除和拖拽的开源库。不过大多是基于View.OnDragListener实现的。还有的自己写touch事件监听,即繁琐又不优雅。RecyclerView的出现,让人耳目一新。拖拽和侧滑的实现当然也不能再low了。
正如用GestureDetectorCompat为RecycleView添加点击事件一样,拖拽和侧滑Google也有针对RecycleView的api。用起来so easy。
onInterceptTouchEvent
onInterceptTouchEvent类,包含在Android Support Library之中。它的功能正如注释所说:
/**
* This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView.
* <p>
* It works with a RecyclerView and a Callback class, which configures what type of interactions
* are enabled and also receives events when user performs these actions.
* <p>
* Depending on which functionality you support, you should override
* {@link Callback#onMove(RecyclerView, ViewHolder, ViewHolder)} and / or
* {@link Callback#onSwiped(ViewHolder, int)}.
* <p>
* This class is designed to work with any LayoutManager but for certain situations, it can be
* optimized for your custom LayoutManager by extending methods in the
* {@link ItemTouchHelper.Callback} class or implementing {@link ItemTouchHelper.ViewDropHandler}
* interface in your LayoutManager.
* <p>
* By default, ItemTouchHelper moves the items' translateX/Y properties to reposition them. On
* platforms older than Honeycomb, ItemTouchHelper uses canvas translations and View's visibility
* property to move items in response to touch events. You can customize these behaviors by
* overriding {@link Callback#onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int,
* boolean)}
* or {@link Callback#onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int,
* boolean)}.
* <p/>
* Most of the time, you only need to override <code>onChildDraw</code> but due to limitations of
* platform prior to Honeycomb, you may need to implement <code>onChildDrawOver</code> as well.
*/
Google为我们实现了两千多行的代码。我们需要关心的只有ItemTouchHelper.Callback接口的三个方法而已。
/**设置我们拖动的方向以及侧滑的方向的*/
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
return 0;
}
/**拖动item时会回调此方法*/
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
/**侧滑item时会回调此方法*/
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
开搞:
第一步,新建SimpleItemTouchHelperCallback类继承ItemTouchHelper.Callback。重写上述三个方法。由于RecycleView的布局有多种,所以我们首先要判断拖拽方向,如果是ListView形式的,就是上下拖拽,如果的GridvView形式的,就上下左右均可拖拽,所以对getMovementFlags重写:
//如果是ListView样式的RecyclerView
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager){
//设置拖拽方向为上下
final int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN;
//设置侧滑方向为从左到右和从右到左都可以
final int swipeFlags = ItemTouchHelper.START|ItemTouchHelper.END;
//将方向参数设置进去
return makeMovementFlags(dragFlags,swipeFlags);
}else{//如果是GridView样式的RecyclerView
//设置拖拽方向为上下左右
final int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN|
ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;
//不支持侧滑
final int swipeFlags = 0;
return makeMovementFlags(dragFlags,swipeFlags);
}
拖拽后位置的变动需要我们的Adapter数据改变才,所以声明一个接口,
public interface onMoveAndSwipedListener {
boolean onItemMove(int fromPosition , int toPosition);
void onItemDismiss(int position);
}
通过构造方法传进来Adapter的监听:
private onMoveAndSwipedListener mAdapter;
public SimpleItemTouchHelperCallback(onMoveAndSwipedListener listener)
{
mAdapter = listener;
}
实现拖拽,在onMove里调用:
//回调adapter中的onItemMove方法
mAdapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
实现侧滑删除,在onSwiped里调用:
//回调adapter中的onItemDismiss方法
mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
ItemTouchHelper就编写好了。。。简单吧。下面Adapter去实现着这个回掉接口:
//还是我们的第一个适配器。。。
implements SimpleItemTouchHelperCallback.onMoveAndSwipedListener{
@Override
public boolean onItemMove(int fromPosition, int toPosition)
{
//交换mItems数据的位置
Collections.swap(mData,fromPosition,toPosition);
//交换RecyclerView列表中item的位置
notifyItemMoved(fromPosition,toPosition);
return true;
}
@Override
public void onItemDismiss(int position)
{
//删除mItems数据
mData.remove(position);
//删除RecyclerView列表对应item
notifyItemRemoved(position);
}
ok。运行试试吧:
想试试多布局?用上篇的MoreItemAdapter实现接口:
public class MoreItemAdapter extends MultiItemTypeAdapter<String> implements SimpleItemTouchHelperCallback.onMoveAndSwipedListener
一样好使。
传送门:darg分支