Skip to content

Commit

Permalink
Merge pull request #127 from elfman/master
Browse files Browse the repository at this point in the history
Enable draw on the edge when swipe moving
  • Loading branch information
CymChad authored Jul 3, 2016
2 parents f65f8b7 + fd11c2f commit 6885839
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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<String> generateData(int size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();

}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.chad.library.adapter.base.listener;

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;

/**
Expand All @@ -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);

}

0 comments on commit 6885839

Please sign in to comment.