1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| public class MyHelperCall extends ItemTouchHelper.Callback {
private final ItemMoveListener mItemMoveListener;
public MyHelperCall(ItemMoveListener listener) { this.mItemMoveListener = listener; }
@Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT; return makeMovementFlags(dragFlags, swipeFlags); }
@Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder) { if (srcHolder.getItemViewType() != targetHolder.getItemViewType()) return false; mItemMoveListener.onMove(srcHolder.getAdapterPosition(), targetHolder.getAdapterPosition()); return true; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { mItemMoveListener.onRemove(viewHolder.getAdapterPosition()); }
@Override public void onSelectedChanged(RecyclerView.ViewHolder holder, int actionState) { if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) { holder.itemView.setBackgroundColor( holder.itemView.getContext().getResources().getColor(R.color.selectColor)); } super.onSelectedChanged(holder, actionState); }
@Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder , float dX, float dY, int actionState, boolean isCurrentlyActive) { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { Log.e("test", "dx:" + dX + ",dY:" + dY); viewHolder.itemView.setAlpha(1 - Math.abs(dX) / viewHolder.itemView.getWidth()); viewHolder.itemView.setScaleX(1 - Math.abs(dX) / viewHolder.itemView.getWidth()); viewHolder.itemView.setScaleY(1 - Math.abs(dX) / viewHolder.itemView.getWidth()); } } @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder holder) { super.clearView(recyclerView, holder); holder.itemView.setBackgroundColor(Color.WHITE); holder.itemView.setAlpha(1); holder.itemView.setScaleX(1); holder.itemView.setScaleY(1); } @Override public boolean isItemViewSwipeEnabled() { return super.isItemViewSwipeEnabled(); }
@Override public boolean isLongPressDragEnabled() { return super.isLongPressDragEnabled(); } }
|