一、RecyclerView设置拖动后怎么监听拖动的开始和结束
ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//首先回调的方法 返回int表示是否监听该方向
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;//拖拽
// int swipeFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;//侧滑删除
return makeMovementFlags(dragFlags, -1);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
//滑动事件
Collections.swap(mList, viewHolder.getAdapterPosition(), target.getAdapterPosition());
mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
} @Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { if (viewHolder == null) {
// onMoveEnd(); //拖动结束
ToastUtil.showShort(mContext,"拖动结束");
mAdapter.notifyDataSetChanged();
} else {
// onMoveStart(); //拖动开始
}
super.onSelectedChanged(viewHolder, actionState);
} @Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
//侧滑事件
// onItemDelete(viewHolder.itemView, viewHolder.getAdapterPosition());
// mCAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
}
@Override
public boolean isLongPressDragEnabled() {
//是否可拖拽
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
//是否可以删除
return false;
}
});
helper.attachToRecyclerView(mRvEdtSort); //设置给RecyleView
重写 onSelectedChanged 方法,并且判断是否拖动的开始和结束。