Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}`
like image 851
Pardeep Kumar Avatar asked Dec 18 '17 07:12

Pardeep Kumar


People also ask

What is the difference between scroll view and recycler view?

In the practical on scrolling views, you use ScrollView to scroll a View or ViewGroup . ScrollView is easy to use, but it's not recommended for long, scrollable lists. RecyclerView is a subclass of ViewGroup and is a more resource-efficient way to display scrollable lists.

How do I drag and drop items in RecyclerView Android?

Drag and Drop can be added in a RecyclerView using the ItemTouchHelper utility class. Following are the important methods in the ItemTouchHelper. Callback interface which needs to be implemented: isLongPressDragEnabled - return true here to enable long press on the RecyclerView rows for drag and drop.


1 Answers

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.

like image 184
s-hunter Avatar answered Sep 22 '22 19:09

s-hunter