so let's start.
1.add a global varaibles in your VideoFolder.java
private Paint mClearPaint;
LinearLayout parentLayout; //assaign this in onCreate Method
2.add this code to your VideoFolder.java
ItemTouchHelper.SimpleCallback simpleCallback =
new ItemTouchHelper.SimpleCallback(0 , ItemTouchHelper.LEFT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder,
@NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
selectionArrayList.add(videoModelArrayList.get(position));
videosAdapter.removeItem(position);
Snackbar snackbar = Snackbar.make(parentLayout, "are you sure..", Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("Yes", new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
boolean isDeleted = selectedFile(selectionArrayList, true);
runOnUiThread(new Runnable() {
@Override
public void run() {
snackbar.dismiss();
if (isDeleted){
selectionArrayList.clear();
}else {
Toast.makeText(VideoFolder.this, "Delete Fail",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
thread.start();
}
});
snackbar.show();
snackbar.setActionTextColor(Color.RED);
}
@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
ColorDrawable mBackground;
int backgroundColor;
Drawable deleteDrawable;
int intrinsicWidth;
int intrinsicHeight;
mBackground = new ColorDrawable();
backgroundColor = Color.parseColor("#b80f0a");
mClearPaint = new Paint();
mClearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
deleteDrawable = ContextCompat.getDrawable(VideoFolder.this, R.drawable.delete);
intrinsicWidth = deleteDrawable.getIntrinsicWidth();
intrinsicHeight = deleteDrawable.getIntrinsicHeight();
View itemView = viewHolder.itemView;
int itemHeight = itemView.getHeight();
boolean isCancelled = dX == 0 && !isCurrentlyActive;
if (isCancelled) {
clearCanvas(c, itemView.getRight() + dX, (float) itemView.getTop(),
(float) itemView.getRight(), (float) itemView.getBottom());
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
return;
}
mBackground.setColor(backgroundColor);
mBackground.setBounds(itemView.getRight() + (int) dX, itemView.getTop(),
itemView.getRight(), itemView.getBottom());
mBackground.draw(c);
int deleteIconTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
int deleteIconMargin = (itemHeight - intrinsicHeight) / 2;
int deleteIconLeft = itemView.getRight() - deleteIconMargin - intrinsicWidth;
int deleteIconRight = itemView.getRight() - deleteIconMargin;
int deleteIconBottom = deleteIconTop + intrinsicHeight;
deleteDrawable.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom);
deleteDrawable.draw(c);
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
};
private void clearCanvas(Canvas c, Float left, Float top,
Float right, Float bottom) {
c.drawRect(left,top,right,bottom, mClearPaint);
}
3.attach ItemTouchHelper to you recylerview like this
new ItemTouchHelper(simpleCallback).attachToRecyclerView(recyclerView);
4.add this method in your videoFolderAdapter.java to remove item
public void removeItem(int position){
videoFolder.remove(position);
notifyItemRemoved(position);
}
Watch video Tutorial
0 Comments