5

Hi I am using recycler view and apply ITEMTOUCHHELPER its working. I apply Logic (direction == ItemTouchHelper.LEFT) then delete item. All of these things work correctly.

But when I swipe right side and then swipe left side. It give dX value >0.which means swiping done on right side.

If I delete an item no issue If I leave it as it is and swipe again then this strange behavior start.

When I swipe multiple times from left side then it gives dX<0 means then it starts again working.

Here is my Implementation

  private void initSwipe(){
        ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | 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();


                if (direction == ItemTouchHelper.LEFT){
                    Cursor cursor = ((BookRecyclerAdapter)recyclerView.getAdapter()).getCursor();
                    cursor.moveToPosition(position);
                    int  pageNo = cursor.getInt(cursor.getColumnIndex(BookMarkContract.AddsEntry.COLUMN_NAME_PAGE_NO));
                    dbHelper.deletePageNo(pageNo);
                    bookRecyclerAdapter.swapCursor(dbHelper.getAllBookMarks());
                    bookRecyclerAdapter.notifyItemRemoved(position);
                    bookRecyclerAdapter.notifyDataSetChanged();

                }
                else
                return;



            }

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

                Bitmap icon;

                Log.d("dX",""+dX);
                if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){
                    if(dX>0)
                    {
                        dX=0;
                    }
                    View itemView = viewHolder.itemView;
                    float height = (float) itemView.getBottom() - (float) itemView.getTop();
                    float width = height / 3;
                    if(dX>0)
                        return;
else {

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

Here is my log cat Snippet of when i Swipping left but it gives me dX >0

PROBLEM IN ONE LINE

SWIPPING LEFT SIDE GIVES dX >0

QUESTION

Why I am getting this strange behavior ? it feels like an app is hanging But it is not hanging it is swiping right side even When I swipe LEFT.

Syed Qasim Ahmed
  • 1,352
  • 13
  • 24

2 Answers2

3

If you only want to allow leftswipe, this is your solution:

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
     //int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
       int swipeFlags = ItemTouchHelper.START;
       return makeMovementFlags(dragFlags, swipeFlags);
}
XxGoliathusxX
  • 922
  • 13
  • 34
0

Here is what I think it is a more cleaner and simpler solution.

When you extend ItemTouchHelper.SimpleCallback in order to get the custom behavior, you should pass the directions for your items to be swiped or dragged, you don't need to override getMovementFlags since this method uses the drag and swipe directions we are about to pass. For example:

public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {

    public RecyclerItemTouchHelper(int dragDirs, int swipeDirs) {
        super(dragDirs, swipeDirs);
    }

    // ...
}

Now, in your activity or fragment, when you setup your recycler view you can specify the swiping and dragging behavior like this:

// 0 means no dragging at all and the 2nd parameters means only swipe left is allowed
RecyclerItemTouchHelper helper = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT);
new ItemTouchHelper(helper).attachToRecyclerView(yourRecyclerView);

Now you can configure your dragging and swiping however you want. I hope it helps!

For more information you can take a look at the official documentation, there's pretty much the exact example you need:

https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.SimpleCallback

Alejandro Casanova
  • 3,633
  • 4
  • 31
  • 46