Modify the behaviour of ItemTouchHelper.

This commit is contained in:
Docile-Alligator 2022-11-07 13:38:55 +11:00
parent fd63dba91b
commit c2360ca59a
4 changed files with 188 additions and 152 deletions

View File

@ -177,6 +177,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
@Override @Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if (touchHelper != null) { if (touchHelper != null) {
exceedThreshold = false;
touchHelper.attachToRecyclerView(null); touchHelper.attachToRecyclerView(null);
touchHelper.attachToRecyclerView(mCommentRecyclerView); touchHelper.attachToRecyclerView(mCommentRecyclerView);
if (mAdapter != null) { if (mAdapter != null) {
@ -189,55 +190,62 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
View itemView = viewHolder.itemView; if (isCurrentlyActive) {
int horizontalOffset = (int) Utils.convertDpToPixel(16, mActivity); View itemView = viewHolder.itemView;
if (dX > 0) { int horizontalOffset = (int) Utils.convertDpToPixel(16, mActivity);
if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { if (dX > 0) {
if (!exceedThreshold) { if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
exceedThreshold = true; if (!exceedThreshold) {
if (vibrateWhenActionTriggered) { exceedThreshold = true;
viewHolder.itemView.setHapticFeedbackEnabled(true); if (vibrateWhenActionTriggered) {
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); viewHolder.itemView.setHapticFeedbackEnabled(true);
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
} }
backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeRight.setBounds(0, 0, 0, 0);
} }
backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeRight.setBounds(0, 0, 0, 0);
}
drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(), drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(),
(itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2, (itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2,
itemView.getLeft() + ((int) dX) - horizontalOffset, itemView.getLeft() + ((int) dX) - horizontalOffset,
(itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2); (itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2);
backgroundSwipeRight.draw(c); backgroundSwipeRight.draw(c);
drawableSwipeRight.draw(c); drawableSwipeRight.draw(c);
} else if (dX < 0) { } else if (dX < 0) {
if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
if (!exceedThreshold) { if (!exceedThreshold) {
exceedThreshold = true; exceedThreshold = true;
if (vibrateWhenActionTriggered) { if (vibrateWhenActionTriggered) {
viewHolder.itemView.setHapticFeedbackEnabled(true); viewHolder.itemView.setHapticFeedbackEnabled(true);
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
} }
backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeLeft.setBounds(0, 0, 0, 0);
} }
backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset,
} else { (itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2,
exceedThreshold = false; itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(),
backgroundSwipeLeft.setBounds(0, 0, 0, 0); (itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2);
backgroundSwipeLeft.draw(c);
drawableSwipeLeft.draw(c);
}
} else {
if (exceedThreshold) {
mAdapter.onItemSwipe(viewHolder, dX > 0 ? ItemTouchHelper.END : ItemTouchHelper.START, swipeLeftAction, swipeRightAction);
exceedThreshold = false;
} }
drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset,
(itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2,
itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(),
(itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2);
backgroundSwipeLeft.draw(c);
drawableSwipeLeft.draw(c);
} }
} }
@Override @Override
public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) { public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
return swipeActionThreshold; return 100;
} }
}); });

View File

@ -488,6 +488,7 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato
@Override @Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if (touchHelper != null) { if (touchHelper != null) {
exceedThreshold = false;
touchHelper.attachToRecyclerView(null); touchHelper.attachToRecyclerView(null);
touchHelper.attachToRecyclerView(mPostRecyclerView); touchHelper.attachToRecyclerView(mPostRecyclerView);
if (mAdapter != null) { if (mAdapter != null) {
@ -500,55 +501,62 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
View itemView = viewHolder.itemView; if (isCurrentlyActive) {
int horizontalOffset = (int) Utils.convertDpToPixel(16, activity); View itemView = viewHolder.itemView;
if (dX > 0) { int horizontalOffset = (int) Utils.convertDpToPixel(16, activity);
if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { if (dX > 0) {
if (!exceedThreshold) { if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
exceedThreshold = true; if (!exceedThreshold) {
if (vibrateWhenActionTriggered) { exceedThreshold = true;
viewHolder.itemView.setHapticFeedbackEnabled(true); if (vibrateWhenActionTriggered) {
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); viewHolder.itemView.setHapticFeedbackEnabled(true);
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
} }
backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeRight.setBounds(0, 0, 0, 0);
} }
backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeRight.setBounds(0, 0, 0, 0);
}
drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(), drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(),
(itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2, (itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2,
itemView.getLeft() + ((int) dX) - horizontalOffset, itemView.getLeft() + ((int) dX) - horizontalOffset,
(itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2); (itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2);
backgroundSwipeRight.draw(c); backgroundSwipeRight.draw(c);
drawableSwipeRight.draw(c); drawableSwipeRight.draw(c);
} else if (dX < 0) { } else if (dX < 0) {
if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
if (!exceedThreshold) { if (!exceedThreshold) {
exceedThreshold = true; exceedThreshold = true;
if (vibrateWhenActionTriggered) { if (vibrateWhenActionTriggered) {
viewHolder.itemView.setHapticFeedbackEnabled(true); viewHolder.itemView.setHapticFeedbackEnabled(true);
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
} }
backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeLeft.setBounds(0, 0, 0, 0);
} }
backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset,
} else { (itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2,
exceedThreshold = false; itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(),
backgroundSwipeLeft.setBounds(0, 0, 0, 0); (itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2);
backgroundSwipeLeft.draw(c);
drawableSwipeLeft.draw(c);
}
} else {
if (exceedThreshold) {
mAdapter.onItemSwipe(viewHolder, dX > 0 ? ItemTouchHelper.END : ItemTouchHelper.START, swipeLeftAction, swipeRightAction);
exceedThreshold = false;
} }
drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset,
(itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2,
itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(),
(itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2);
backgroundSwipeLeft.draw(c);
drawableSwipeLeft.draw(c);
} }
} }
@Override @Override
public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) { public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
return swipeActionThreshold; return 100;
} }
}); });

View File

@ -1075,6 +1075,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Override @Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if (touchHelper != null) { if (touchHelper != null) {
exceedThreshold = false;
touchHelper.attachToRecyclerView(null); touchHelper.attachToRecyclerView(null);
touchHelper.attachToRecyclerView(mPostRecyclerView); touchHelper.attachToRecyclerView(mPostRecyclerView);
if (mAdapter != null) { if (mAdapter != null) {
@ -1087,55 +1088,64 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
View itemView = viewHolder.itemView; if (isCurrentlyActive) {
int horizontalOffset = (int) Utils.convertDpToPixel(16, activity); View itemView = viewHolder.itemView;
if (dX > 0) { int horizontalOffset = (int) Utils.convertDpToPixel(16, activity);
if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { if (dX > 0) {
if (!exceedThreshold) { if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
exceedThreshold = true; if (!exceedThreshold) {
if (vibrateWhenActionTriggered) { exceedThreshold = true;
viewHolder.itemView.setHapticFeedbackEnabled(true); if (vibrateWhenActionTriggered) {
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); viewHolder.itemView.setHapticFeedbackEnabled(true);
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
} }
backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeRight.setBounds(0, 0, 0, 0);
} }
backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeRight.setBounds(0, 0, 0, 0);
}
drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(), drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(),
(itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2, (itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2,
itemView.getLeft() + ((int) dX) - horizontalOffset, itemView.getLeft() + ((int) dX) - horizontalOffset,
(itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2); (itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2);
backgroundSwipeRight.draw(c); backgroundSwipeRight.draw(c);
drawableSwipeRight.draw(c); drawableSwipeRight.draw(c);
} else if (dX < 0) { } else if (dX < 0) {
if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
if (!exceedThreshold) { if (!exceedThreshold) {
exceedThreshold = true; exceedThreshold = true;
if (vibrateWhenActionTriggered) { if (vibrateWhenActionTriggered) {
viewHolder.itemView.setHapticFeedbackEnabled(true); viewHolder.itemView.setHapticFeedbackEnabled(true);
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
} }
backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeLeft.setBounds(0, 0, 0, 0);
} }
backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset,
} else { (itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2,
exceedThreshold = false; itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(),
backgroundSwipeLeft.setBounds(0, 0, 0, 0); (itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2);
backgroundSwipeLeft.draw(c);
drawableSwipeLeft.draw(c);
}
} else {
if (exceedThreshold) {
mAdapter.onItemSwipe(viewHolder, dX > 0 ? ItemTouchHelper.END : ItemTouchHelper.START, swipeLeftAction, swipeRightAction);
exceedThreshold = false;
} }
drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset,
(itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2,
itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(),
(itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2);
backgroundSwipeLeft.draw(c);
drawableSwipeLeft.draw(c);
} }
} }
@Override @Override
public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) { public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
return swipeActionThreshold; return 100;
} }
}); });

View File

@ -458,6 +458,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
@Override @Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if (touchHelper != null) { if (touchHelper != null) {
exceedThreshold = false;
touchHelper.attachToRecyclerView(null); touchHelper.attachToRecyclerView(null);
touchHelper.attachToRecyclerView((mCommentsRecyclerView == null ? mRecyclerView : mCommentsRecyclerView)); touchHelper.attachToRecyclerView((mCommentsRecyclerView == null ? mRecyclerView : mCommentsRecyclerView));
if (mCommentsAdapter != null) { if (mCommentsAdapter != null) {
@ -470,55 +471,64 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
View itemView = viewHolder.itemView; if (isCurrentlyActive) {
int horizontalOffset = (int) Utils.convertDpToPixel(16, activity); View itemView = viewHolder.itemView;
if (dX > 0) { int horizontalOffset = (int) Utils.convertDpToPixel(16, activity);
if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { if (dX > 0) {
if (!exceedThreshold) { if (dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
exceedThreshold = true; if (!exceedThreshold) {
if (vibrateWhenActionTriggered) { exceedThreshold = true;
viewHolder.itemView.setHapticFeedbackEnabled(true); if (vibrateWhenActionTriggered) {
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); viewHolder.itemView.setHapticFeedbackEnabled(true);
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
} }
backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeRight.setBounds(0, 0, 0, 0);
} }
backgroundSwipeRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeRight.setBounds(0, 0, 0, 0);
}
drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(), drawableSwipeRight.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableSwipeRight.getIntrinsicWidth(),
(itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2, (itemView.getBottom() + itemView.getTop() - drawableSwipeRight.getIntrinsicHeight()) / 2,
itemView.getLeft() + ((int) dX) - horizontalOffset, itemView.getLeft() + ((int) dX) - horizontalOffset,
(itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2); (itemView.getBottom() + itemView.getTop() + drawableSwipeRight.getIntrinsicHeight()) / 2);
backgroundSwipeRight.draw(c); backgroundSwipeRight.draw(c);
drawableSwipeRight.draw(c); drawableSwipeRight.draw(c);
} else if (dX < 0) { } else if (dX < 0) {
if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) { if (-dX > (itemView.getRight() - itemView.getLeft()) * swipeActionThreshold) {
if (!exceedThreshold) { if (!exceedThreshold) {
exceedThreshold = true; exceedThreshold = true;
if (vibrateWhenActionTriggered) { if (vibrateWhenActionTriggered) {
viewHolder.itemView.setHapticFeedbackEnabled(true); viewHolder.itemView.setHapticFeedbackEnabled(true);
viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); viewHolder.itemView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
}
} }
backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else {
exceedThreshold = false;
backgroundSwipeLeft.setBounds(0, 0, 0, 0);
} }
backgroundSwipeLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom()); drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset,
} else { (itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2,
exceedThreshold = false; itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(),
backgroundSwipeLeft.setBounds(0, 0, 0, 0); (itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2);
backgroundSwipeLeft.draw(c);
drawableSwipeLeft.draw(c);
}
} else {
if (exceedThreshold) {
if (mCommentsAdapter != null) {
mCommentsAdapter.onItemSwipe(viewHolder, dX > 0 ? ItemTouchHelper.END : ItemTouchHelper.START, swipeLeftAction, swipeRightAction);
}
exceedThreshold = false;
} }
drawableSwipeLeft.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset,
(itemView.getBottom() + itemView.getTop() - drawableSwipeLeft.getIntrinsicHeight()) / 2,
itemView.getRight() + ((int) dX) + horizontalOffset + drawableSwipeLeft.getIntrinsicWidth(),
(itemView.getBottom() + itemView.getTop() + drawableSwipeLeft.getIntrinsicHeight()) / 2);
backgroundSwipeLeft.draw(c);
drawableSwipeLeft.draw(c);
} }
} }
@Override @Override
public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) { public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
return swipeActionThreshold; return 100;
} }
}); });