Skip to content

Instantly share code, notes, and snippets.

@rishabh876
Last active August 26, 2022 04:08
Show Gist options
  • Save rishabh876/859cbe8af2627f00abf0bd1f84b6533f to your computer and use it in GitHub Desktop.
Save rishabh876/859cbe8af2627f00abf0bd1f84b6533f to your computer and use it in GitHub Desktop.
SwipeItemTouchHelper

SwipeItemTouchHelper

public class SwipeItemTouchHelper extends ItemTouchHelper.SimpleCallback {

    private Context context;

    private SwipeItemActionListener swipeItemActionListener;
    private ItemTouchHelper itemTouchHelper;
    private SwipeBackgroundView swipeBackgroundView; //View that will be shown behind the item

    public SwipeItemTouchHelper(Context context) {
        super(0, ItemTouchHelper.START);
        this.context = context;
        itemTouchHelper = new ItemTouchHelper(this);
    }

    public void setUIAndListener(String swipeText, @DrawableRes int swipeStatesDrawable, SwipeItemActionListener swipeItemActionListener) {
        this.swipeItemActionListener = swipeItemActionListener;
        swipeBackgroundView = new SwipeBackgroundView(context, getSwipeThreshold(), swipeText, swipeStatesDrawable);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView,
                          RecyclerView.ViewHolder viewHolder,
                          RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
        //progress loader footer should not be swipe-able
        if (viewHolder instanceof FooterViewHolder) {
            return;
        }
        
        swipeItemActionListener.onSwipedItem(viewHolder);
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {

        //progress loader footer should not be swipe-able
        if (viewHolder instanceof FooterViewHolder) {
            super.onChildDraw(c, recyclerView, viewHolder, 0, 0, actionState, false);
            return;
        }

        if (DeviceUtil.isLTRLayout(viewHolder.itemView)) {
            c.translate(Math.round(viewHolder.itemView.getRight() + dX), viewHolder.itemView.getTop());
        } else {
            c.translate(Math.round(dX - viewHolder.itemView.getRight()), viewHolder.itemView.getTop());
        }

        swipeBackgroundView.updateSwipeView(viewHolder.itemView, dX);
        swipeBackgroundView.draw(c);

        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

    @Override
    public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
        return getSwipeThreshold();
    }

    private float getSwipeThreshold() {
        if (DeviceUtil.isTablet()) {
            return .2f;
        } else {
            return .4f;
        }
    }

    public ItemTouchHelper getItemTouchHelper() {
        return itemTouchHelper;
    }

    public interface SwipeItemActionListener {

        void onSwipedItem(RecyclerView.ViewHolder viewHolder);
    }
}

SwipeBackgroundView

/**
*View that will be shown in background
**/
public class SwipeBackgroundView extends LinearLayout {

    private AppCompatCheckedTextView actionTextView;
    private float swipeThreshold;

    public SwipeBackgroundView(Context context, float swipeThreshold, String swipeText, @DrawableRes int backgroundDrawable) {
        super(context);
        View view = inflate(getContext(), R.layout.swipe_background_view, this);
        actionTextView = (AppCompatCheckedTextView) view.findViewById(R.id.tv_swipe_background_view);
        actionTextView.setText(swipeText);
        actionTextView.setBackgroundResource(backgroundDrawable);
        this.swipeThreshold = swipeThreshold;
    }

    public void updateSwipeView(View parentView, float dX) {

        int viewWidth = parentView.getWidth();
        int viewHeight = parentView.getHeight();

        LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(viewWidth, viewHeight);

        actionTextView.setLayoutParams(relativeParams);

        float currentSwipeThreshold = Math.abs(dX) / viewWidth;
        if (currentSwipeThreshold > swipeThreshold) {
            actionTextView.setChecked(true);
        } else {
            actionTextView.setChecked(false);
        }

        invalidate();
        measure(viewWidth, viewHeight);
        if (DeviceUtil.isLTRLayout(parentView)) {
            layout(Math.round(parentView.getRight() + dX), parentView.getTop(), parentView.getRight(), parentView.getBottom());
        } else {
            layout(parentView.getLeft(), parentView.getTop(), Math.round(parentView.getLeft() + dX), parentView.getBottom());
        }
    }

    private SwipeBackgroundView(Context context) {
        super(context);
    }
}

How to use

SwipeItemTouchHelper swipeItemTouchHelper = new SwipeItemTouchHelper(getContext());
swipeItemTouchHelper.setUIAndListener(swipeText,
                                              swipeBackgroundDrawableId,
                                              swipeItemActionListener);
swipeItemTouchHelper.getItemTouchHelper().attachToRecyclerView(recycleView);
@xcodepyx
Copy link

i use apk editor how can i use it in samli help me please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment