11

I have a Recycler View in android. I have attached an ItemTouchHelper to it.I have enabled only swiping from left to right.

So when i swipe any item of Recycler view , the item starts moving towards right and on the left i draw a text. This is fine . My requirement is that, I want allow user to swipe only to some distance , and when that distance is reached and user release the touch, the item being swiped should go back to its position towards left.

Problem is that , when i swipe left to right the view gets fully swiped out of the screen . How can i restrict it to be swiped only to some distance ? How do i do it ?

Here is my code for the item touch callback:

private void initSwipe(){
    ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                int position = viewHolder.getAdapterPosition();
            }

        @Override
        public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
            ViewHolder viewHolder1 = (ViewHolder)viewHolder;
            int position = viewHolder1.getAdapterPosition();
            MyItem item = dataList.get(position);
            if (item==null )return;
            if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){

                View itemView = viewHolder.itemView;
                float height = (float) itemView.getBottom() - (float) itemView.getTop();
                float width = height / 3;
                Paint p = new Paint();
                Paint textPaint = new Paint();
                textPaint.setColor(Color.WHITE);
                textPaint.setTextSize(20);

                if(dX > 0) {
                    p.setColor(Color.parseColor("#000000"));
                    RectF background = new RectF((float) itemView.getLeft(), (float) itemView.getTop(), dX,(float) itemView.getBottom());
                    c.drawRect(background,p);
                    //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);
                    c.drawText(item.getDate(),(float)(itemView.getLeft() + width),(float)(itemView.getBottom() - width), textPaint);
                }
            }
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }
    };
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    itemTouchHelper.attachToRecyclerView(myRecycler);
}`
Navneet Krishna
  • 4,667
  • 5
  • 21
  • 40
Pardeep Kumar
  • 730
  • 10
  • 27

1 Answers1

10

Replace this line in the onChildDraw:

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

with the following, it sets the max x position to be at 100, adjust this value to your need.

float newDx = dX;
if (newDx >= 100f) {
    newDx = 100f
}
super.onChildDraw(c, recyclerView, viewHolder, newDx, dY, actionState, isCurrentlyActive);

Then in the callback handler's onSwiped, call notifyItemChanged on the RecyclerView adapter.

 @Override
 public void onSwiped(RecyclerView.ViewHolder viewHolder,
     int direction) {
         if (direction == ItemTouchHelper.RIGHT) {
        adapter.notifyItemChanged(viewHolder.getAdapterPosition())
    }
 }

A slight issue with notifyItemChanged is that you might notice a quick flash when the view is getting restored to it's original position.

s-hunter
  • 17,762
  • 11
  • 67
  • 105
  • 1
    Can we swipe it back to its original position without blinking ? – Sandeep Dhami Aug 25 '19 at 14:31
  • 1
    This works, but how to reduce swipe speed before it reach end like WhatsApp? – Gunaseelan Jul 29 '20 at 08:12
  • You can also control it by pasting this code snippet in your onChildDraw method `if (actionState == ItemTouchHelper.ActionStateSwipe) { if (dX < 0) { itemView.TranslationX = dX / 5; } } else { base.OnChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); }` //`itemView` is `viewHolder.ItemView` – AZ_ Sep 24 '20 at 10:54