ItemTouchHelper内部包含内部类ItemTouchHelper.Callback
public class ItemTouchHelper extends RecyclerView.ItemDecoration implements RecyclerView.OnChildAttachStateChangeListener {
public abstract static class Callback {
/** @param viewHolder 正在被拖拽的viewHolder. * @param target 被正在拖拽的覆盖的viewHolder * dragged.*/
public abstract boolean onMove(RecyclerView recyclerView,ViewHolder viewHolder, ViewHolder target);
/**direction 滑动的方向 * {@link #UP}, {@link #DOWN}, * {@link #LEFT} or {@link #RIGHT}*/
public abstract void onSwiped(ViewHolder viewHolder, int direction);
/**makeFlag(ACTION_STATE_IDLE, RIGHT) | makeFlag(ACTION_STATE_SWIPE, LEFT | RIGHT);*/
public abstract int getMovementFlags(RecyclerView recyclerView,ViewHolder viewHolder);
/**被RecyclerView's onDraw调用*/
public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { sUICallback.onDraw(c, recyclerView, viewHolder.itemView, dX, dY, actionState, isCurrentlyActive); }
/**被RecyclerView's onDraw调用,比较少用到*/
public void onChildDrawOver(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { sUICallback.onDrawOver(c, recyclerView, viewHolder.itemView, dX, dY, actionState, isCurrentlyActive); }
public boolean isLongPressDragEnabled() { return true; }
public boolean isItemViewSwipeEnabled() { return true; }
/**当拖拽或者滑动改变的时候调用,复写是需调用父类方法*/
public void onSelectedChanged(ViewHolder viewHolder, int actionState) { if (viewHolder != null) { sUICallback.onSelected(viewHolder.itemView); } }
……
}
/**默认Item长按时触发拖拽功能*/
public void startDrag(ViewHolder viewHolder) { if (!mCallback.hasDragFlag(mRecyclerView, viewHolder)) { Log.e(TAG, "Start drag has been called but swiping is not enabled"); return; } if (viewHolder.itemView.getParent() != mRecyclerView) { Log.e(TAG, "Start drag has been called with a view holder which is not a child of " + "the RecyclerView which is controlled by this ItemTouchHelper."); return; } obtainVelocityTracker(); mDx = mDy = 0f; select(viewHolder, ACTION_STATE_DRAG); }
public static interface ViewDropHandler {……}
}
public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback { public static final float ALPHA_FULL = 1.0f; private final ItemTouchHelperAdapter mAdapter; public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) { mAdapter = adapter; } @Override public boolean isLongPressDragEnabled() { return true; } @Override public boolean isItemViewSwipeEnabled() { return true; } @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { if (recyclerView.getLayoutManager() instanceof GridLayoutManager) { final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; final int swipeFlags = 0; return makeMovementFlags(dragFlags, swipeFlags); } else { final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; return makeMovementFlags(dragFlags, swipeFlags); } } @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) { if (source.getItemViewType() != target.getItemViewType()) { return false; } // Notify the adapter of the move mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition()); return true; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) { // Notify the adapter of the dismissal mAdapter.onItemDismiss(viewHolder.getAdapterPosition()); } @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { // Fade out the view as it is swiped out of the parent's bounds final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth(); viewHolder.itemView.setAlpha(alpha); viewHolder.itemView.setTranslationX(dX); } else { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } } @Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { // We only want the active item to change if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) { if (viewHolder instanceof ItemTouchHelperViewHolder) { // Let the view holder know that this item is being moved or dragged ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder; itemViewHolder.onItemSelected(); } } super.onSelectedChanged(viewHolder, actionState); } @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { super.clearView(recyclerView, viewHolder); viewHolder.itemView.setAlpha(ALPHA_FULL); if (viewHolder instanceof ItemTouchHelperViewHolder) { ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder; itemViewHolder.onItemClear(); } } }