Syed Qasim Ahmed Syed Qasim Ahmed - 3 months ago 29
Android Question

ItemTouchHelper not working properly android

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 the swipe left side.It give dX value >0.which means swipping done on right side.

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

When i multiple time swipe 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 behaviour ? it feels a user like app swipe hanging
But it is not hanging it is wipping right side even i swipe LEFT.

Answer

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