Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict ItemtouchHelper to swipe only left to right

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.

like image 255
Syed Qasim Ahmed Avatar asked Oct 18 '22 03:10

Syed Qasim Ahmed


1 Answers

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);
}
like image 141
XxGoliathusxX Avatar answered Oct 21 '22 05:10

XxGoliathusxX