Java tutorial
/* * Copyright (C) 2016 Kelvin Leung Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.androidarchitecture.widgets; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.support.v4.content.ContextCompat; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; import android.view.View; import com.androidarchitecture.R; public class RecyclerViewSwipeToDeleteImpl { private Context mContext; private RecyclerView mRecyclerView; private SwipeToDeleteListener mListener; public RecyclerViewSwipeToDeleteImpl(RecyclerView recyclerView, SwipeToDeleteListener listener) { this.mContext = recyclerView.getContext(); this.mRecyclerView = recyclerView; this.mListener = listener; setUpItemTouchHelper(); setUpAnimationDecoratorHelper(); } /** * This is the standard support library way of implementing "swipe to delete" feature. You can do custom drawing in onChildDraw method * but whatever you draw will disappear once the swipe is over, and while the items are animating to their new position the recycler view * background will be visible. That is rarely an desired effect. */ private void setUpItemTouchHelper() { ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) { // we want to cache these and not allocate anything repeatedly in the onChildDraw method Drawable background; Drawable xMark; int xMarkMargin; boolean initiated; private void init() { background = new ColorDrawable(ContextCompat.getColor(mContext, R.color.colorPrimary)); xMark = ContextCompat.getDrawable(mContext, R.drawable.ic_cancel_black_24dp); xMark.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP); xMarkMargin = (int) mContext.getResources().getDimension(R.dimen.activity_margin); initiated = true; } // not important, we don't want drag & drop @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { return false; } @Override public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { return super.getSwipeDirs(recyclerView, viewHolder); } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) { int swipedPosition = viewHolder.getAdapterPosition(); mListener.removeItem(swipedPosition); } @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { View itemView = viewHolder.itemView; // not sure why, but this method get's called for viewHolder that are already swiped away if (viewHolder.getAdapterPosition() == -1) { // not interested in those return; } if (!initiated) { init(); } // draw red background background.setBounds(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom()); background.draw(c); // draw x mark int itemHeight = itemView.getBottom() - itemView.getTop(); int intrinsicWidth = xMark.getIntrinsicWidth(); int intrinsicHeight = xMark.getIntrinsicWidth(); int xMarkLeft = itemView.getRight() - xMarkMargin - intrinsicWidth; int xMarkRight = itemView.getRight() - xMarkMargin; int xMarkTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2; int xMarkBottom = xMarkTop + intrinsicHeight; xMark.setBounds(xMarkLeft, xMarkTop, xMarkRight, xMarkBottom); xMark.draw(c); super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } }; ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback); mItemTouchHelper.attachToRecyclerView(mRecyclerView); } /** * We're gonna setup another ItemDecorator that will draw the red background in the empty space while the items are animating to their new positions * after an item is removed. */ private void setUpAnimationDecoratorHelper() { mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() { // we want to cache this and not allocate anything repeatedly in the onDraw method Drawable background; boolean initiated; private void init() { background = new ColorDrawable(ContextCompat.getColor(mContext, R.color.colorPrimary)); initiated = true; } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { if (!initiated) { init(); } // only if animation is in progress if (parent.getItemAnimator().isRunning()) { // some items might be animating down and some items might be animating up to close the gap left by the removed item // this is not exclusive, both movement can be happening at the same time // to reproduce this leave just enough items so the first one and the last one would be just a little off screen // then remove one from the middle // find first child with translationY > 0 // and last one with translationY < 0 // we're after a rect that is not covered in recycler-view views at this point in time View lastViewComingDown = null; View firstViewComingUp = null; // this is fixed int left = 0; int right = parent.getWidth(); // this we need to find out int top = 0; int bottom = 0; // find relevant translating views int childCount = parent.getLayoutManager().getChildCount(); for (int i = 0; i < childCount; i++) { View child = parent.getLayoutManager().getChildAt(i); if (child.getTranslationY() < 0) { // view is coming down lastViewComingDown = child; } else if (child.getTranslationY() > 0) { // view is coming up if (firstViewComingUp == null) { firstViewComingUp = child; } } } if (lastViewComingDown != null && firstViewComingUp != null) { // views are coming down AND going up to fill the void top = lastViewComingDown.getBottom() + (int) lastViewComingDown.getTranslationY(); bottom = firstViewComingUp.getTop() + (int) firstViewComingUp.getTranslationY(); } else if (lastViewComingDown != null) { // views are going down to fill the void top = lastViewComingDown.getBottom() + (int) lastViewComingDown.getTranslationY(); bottom = lastViewComingDown.getBottom(); } else if (firstViewComingUp != null) { // views are coming up to fill the void top = firstViewComingUp.getTop(); bottom = firstViewComingUp.getTop() + (int) firstViewComingUp.getTranslationY(); } background.setBounds(left, top, right, bottom); background.draw(c); } super.onDraw(c, parent, state); } }); } public interface SwipeToDeleteListener { void removeItem(int position); } }