androidandroid-recyclerviewitemtouchhelper

How to Stop Swiping the item in Recycler view after reaching some distance


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);
}`

Solution

  • 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.