当用户在 ItemTouchHelper 中点击 ChildDraw 上的“编辑”图标时,任何人都可以教我如何添加动作事件?
目前,当用户滑动项目时,它会显示背景颜色和图标。当用户向右滑动并点击“R.drawable.ic_vector_edit”图像时,我想添加动作。
无论如何我可以添加事件吗?...
ItemTouchHelper helper = new ItemTouchHelper(
new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
final int fromPos = viewHolder.getAdapterPosition();
final int toPos = target.getAdapterPosition();
adapter.notifyItemMoved(fromPos, toPos);
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// RIGHT to LEFT :: delete item
if (direction == ItemTouchHelper.LEFT) {
final int fromPos = viewHolder.getAdapterPosition();
Item myItem = adapter.getItemAtPosition(fromPos);
// delete the item
mItemViewModel.deleteItem(myItem);
}
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
float mdX = dX;
Bitmap bitmap;
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
// Get RecyclerView item from the ViewHolder
View itemView = viewHolder.itemView;
Paint p = new Paint();
float width = itemView.getHeight() / 5;
// LEFT to RIGHT :: edit item
if (dX > 0) {
mdX = dX / 5;
/* Set color for positive displacement */
int color = ContextCompat.getColor(getContext(), R.color.colorDarKGreen);
p.setColor(color);
// Draw Rect with varying right side, equal to displacement dX
c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), mdX,
(float) itemView.getBottom(), p);
bitmap = ResourceUtil.getBitmap(getContext(), R.drawable.ic_vector_edit);
float height = (itemView.getHeight() / 2) - (bitmap.getHeight() / 2);
c.drawBitmap(bitmap, (float) itemView.getLeft() + width, (float) itemView.getTop() + height, p);
} else { // RIGHT to LEFT :: delete item
/* Set color for negative displacement */
int color = ContextCompat.getColor(getContext(), R.color.colorDarkRed);
p.setColor(color);
// Draw Rect with varying left side, equal to the item's right side plus negative displacement dX
c.drawRect((float) itemView.getRight() + dX, (float) itemView.getTop(),
(float) itemView.getRight(), (float) itemView.getBottom(), p);
bitmap = ResourceUtil.getBitmap(getContext(), R.drawable.ic_vector_delete);
float height = (itemView.getHeight() / 2) - (bitmap.getHeight() / 2);
c.drawBitmap(bitmap, (float) itemView.getRight() - (bitmap.getWidth() + width), (float) itemView.getTop() + height, p);
}
super.onChildDraw(c, recyclerView, viewHolder, mdX, dY, actionState, isCurrentlyActive);
}
}
});