ItemTouchHelper : Limit swipe width of ItemTouchHelper.SimpleCallBack on RecyclerView
Asked Answered
S

1

20

I have successfully implemented swipe behavior and performed some actions with it. The problem now I have is I want to limit the swipe width when I swipe the item.

Currently this is what is happening

enter image description here

But I want to limit the swipe behaviour width till here

enter image description here

This is what I have done so far :-

private void swipeExample(){
        ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
//                final int fromPos = viewHolder.getAdapterPosition();
//                final int toPos = target.getAdapterPosition();
                return false;
            }

            @Override
            public void onSwiped(final RecyclerView.ViewHolder viewHolder, int swipeDir) {

                // Show delete confirmation if swipped left
                if (swipeDir == ItemTouchHelper.LEFT) {

                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setMessage("Are you sure you want to delete?")
                            .setCancelable(false)
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    deleteItem(viewHolder);
//                                    getActivity().finish();
                                }
                            })
                            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            });
                    AlertDialog alert = builder.create();
                    alert.show();

                } else if (swipeDir == ItemTouchHelper.RIGHT) {
                    // Show edit dialog
                }
            }

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

                Bitmap icon;
                if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {

                    View itemView = viewHolder.itemView;
                    float height = (float) itemView.getBottom() - (float) itemView.getTop();
                    float width = height / 3;

                    if (dX > 0) {
                        p.setColor(Color.parseColor("#388E3C"));
                        RectF background = new RectF((float) itemView.getLeft(), (float) itemView.getTop(), dX, (float) itemView.getBottom());
                        c.drawRect(background, p);
                        icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_mode_edit_white_24dp);
                        RectF icon_dest = new RectF((float) itemView.getLeft() + width, (float) itemView.getTop() + width, (float) itemView.getLeft() + 2 * width, (float) itemView.getBottom() - width);
                        c.drawBitmap(icon, null, icon_dest, p);
                    } else if (dX < 0) {
                        p.setColor(Color.parseColor("#D32F2F"));
                        RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
                        c.drawRect(background, p);
                        icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_delete_white_24dp);
                        RectF icon_dest = new RectF((float) itemView.getRight() - 2 * width, (float) itemView.getTop() + width, (float) itemView.getRight() - width, (float) itemView.getBottom() - width);
                        c.drawBitmap(icon, null, icon_dest, p);
                    }
                }
                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }
        };

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);

    }

Please could you tell me how to limit the width of the onChildDraw() method.

Thanks in advance.

Sternick answered 7/12, 2016 at 10:32 Comment(2)
doesn't anyone know how to do this? I know it doesn't require a lot of coding but I just can't get into the logics.Sternick
viper have you found some way to do that?Snob
W
48

Your onChildDraw() method at the end calls its super super method like so:

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

To limit the maximum width of your swipe you can invoke super.onChildDraw() with a reduced dX:

super.onChildDraw(c, recyclerView, viewHolder, dX / 4, dY, actionState, isCurrentlyActive);
Whiteness answered 23/2, 2018 at 9:38 Comment(7)
Well this was a question I asked very long time ago. Now I have used "com.daimajia.swipe.SwipeLayout" library.And your answer seems appropriate to my question. Thank you.Sternick
this works well but the speed of the animation takes a big hit. How can we make this work without affecting the animation speed ?Oceangoing
@Oceangoing i need to speed the animation did you solve it ?Lucillalucille
Yes I did. Will share the solution soonOceangoing
@Oceangoing could you please share your solution?Reld
@Oceangoing I'm looking forward your solution for the animation problem =)Mcripley
@Alexander Thiel's can we have both like dx/4 and dx i means to say that .25 Threshold and .5 i want to delete in both way please help me in thisSeabury

© 2022 - 2024 — McMap. All rights reserved.