diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemDragAndSwipeUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemDragAndSwipeUseActivity.java index c23d17ab0..3d0d404d1 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemDragAndSwipeUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/ItemDragAndSwipeUseActivity.java @@ -1,7 +1,9 @@ package com.chad.baserecyclerviewadapterhelper; import android.app.Activity; +import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.Paint; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.support.v7.widget.CardView; @@ -60,6 +62,10 @@ public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) { ((CardView)viewHolder.itemView).setCardBackgroundColor(Color.WHITE); } }; + final Paint paint = new Paint(); + paint.setAntiAlias(true); + paint.setTextSize(20); + paint.setColor(Color.BLACK); OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() { @Override public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) { @@ -81,16 +87,27 @@ public void clearView(RecyclerView.ViewHolder viewHolder, int pos) { public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) { Log.d(TAG, "View Swiped: " + pos); } + + @Override + public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) { + canvas.drawColor(ContextCompat.getColor(ItemDragAndSwipeUseActivity.this, R.color.color_light_blue)); + canvas.drawText("Just some text", 0, 40, paint); + } }; + mAdapter = new ItemDragAdapter(mData); mItemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter); mItemTouchHelper = new ItemTouchHelper(mItemDragAndSwipeCallback); + mItemTouchHelper.attachToRecyclerView(mRecyclerView); + + //mItemDragAndSwipeCallback.setDragMoveFlags(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN); + mItemDragAndSwipeCallback.setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END); mAdapter.enableSwipeItem(); mAdapter.setOnItemSwipeListener(onItemSwipeListener); mAdapter.enableDragItem(mItemTouchHelper); mAdapter.setOnItemDragListener(listener); + mRecyclerView.setAdapter(mAdapter); - mItemTouchHelper.attachToRecyclerView(mRecyclerView); } private List generateData(int size) { diff --git a/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java b/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java index 1a53b0494..2b59ffacc 100755 --- a/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java +++ b/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java @@ -3,6 +3,7 @@ import android.animation.Animator; import android.content.Context; +import android.graphics.Canvas; import android.support.annotation.IntDef; import android.support.annotation.NonNull; import android.support.v4.view.MotionEventCompat; @@ -1077,4 +1078,10 @@ public void onItemSwiped(RecyclerView.ViewHolder viewHolder) { notifyItemRemoved(viewHolder.getAdapterPosition()); } + public void onItemSwiping(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) { + if (mOnItemSwipeListener != null && itemSwipeEnabled) { + mOnItemSwipeListener.onItemSwipeMoving(canvas, viewHolder, dX, dY, isCurrentlyActive); + } + } + } diff --git a/library/src/main/java/com/chad/library/adapter/base/callback/ItemDragAndSwipeCallback.java b/library/src/main/java/com/chad/library/adapter/base/callback/ItemDragAndSwipeCallback.java index f1705e748..1a3f0f91c 100644 --- a/library/src/main/java/com/chad/library/adapter/base/callback/ItemDragAndSwipeCallback.java +++ b/library/src/main/java/com/chad/library/adapter/base/callback/ItemDragAndSwipeCallback.java @@ -1,7 +1,9 @@ package com.chad.library.adapter.base.callback; +import android.graphics.Canvas; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; +import android.view.View; import com.chad.library.adapter.base.BaseQuickAdapter; @@ -140,4 +142,26 @@ public void setDragMoveFlags(int dragMoveFlags) { public void setSwipeMoveFlags(int swipeMoveFlags) { mSwipeMoveFlags = swipeMoveFlags; } + + @Override + public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { + super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); + + if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { + View itemView = viewHolder.itemView; + + c.save(); + if (dX > 0) { + c.clipRect(itemView.getLeft(), itemView.getTop(), itemView.getLeft() + dX, itemView.getBottom()); + c.translate(itemView.getLeft(), itemView.getTop()); + } else { + c.clipRect(itemView.getRight() + dX, itemView.getTop(), itemView.getRight(), itemView.getBottom()); + c.translate(itemView.getRight() + dX, itemView.getTop()); + } + + mAdapter.onItemSwiping(c, viewHolder, dX, dY, isCurrentlyActive); + c.restore(); + + } + } } diff --git a/library/src/main/java/com/chad/library/adapter/base/listener/OnItemSwipeListener.java b/library/src/main/java/com/chad/library/adapter/base/listener/OnItemSwipeListener.java index 658547f05..983b59c7b 100644 --- a/library/src/main/java/com/chad/library/adapter/base/listener/OnItemSwipeListener.java +++ b/library/src/main/java/com/chad/library/adapter/base/listener/OnItemSwipeListener.java @@ -1,5 +1,6 @@ package com.chad.library.adapter.base.listener; +import android.graphics.Canvas; import android.support.v7.widget.RecyclerView; /** @@ -23,5 +24,16 @@ public interface OnItemSwipeListener { */ void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos); + /** + * Draw on the empty edge when swipe moving + * @param canvas the empty edge's canvas + * @param viewHolder The ViewHolder which is being interacted by the User or it was + * interacted and simply animating to its original position + * @param dX The amount of horizontal displacement caused by user's action + * @param dY The amount of vertical displacement caused by user's action + * @param isCurrentlyActive True if this view is currently being controlled by the user or + * false it is simply animating back to its original state. + */ + void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive); }