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

时间:2017-12-18 07:23:15

标签: android android-recyclerview itemtouchhelper

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

1 个答案:

答案 0 :(得分:3)

在onChildDraw中替换此行:

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

如下所示,它将max x位置设置为100,根据需要调整此值。

float newDx = dX;
if (newDx >= 100f) {
    newDx = 100f
}
super.onChildDraw(c, recyclerView, viewHolder, newDx, dY, actionState, isCurrentlyActive);
回调处理程序中的

Then onSwiped,在RecyclerView适配器上调用notifyItemChanged。

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

notifyItemChanged的一个小问题是,当视图恢复到其原始位置时,您可能会注意到快速闪烁。