如何在RecyclerView的ItemTouchHelper中收听点击事件?

时间:2018-10-26 06:19:47

标签: java android android-recyclerview itemtouchhelper

任何人都可以教我如何在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);
                    }
                }
            });

1 个答案:

答案 0 :(得分:0)

尝试使用setOnTouchListener上的RecyclerView来监听触摸事件,如下所示:

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

    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
         ...

         if (dX > 0) {
             ...

             recyclerView.setOnTouchListener((v, event) -> {
                 int left = itemView.getLeft() + width;
                 int top = itemView.getTop() + height;
                 int right = left + bitmap.getWidth();
                 int bottom = top + bitmap.getHeight();

                 if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE &&
                     event.getAction() == MotionEvent.ACTION_DOWN &&
                     event.getX() >= left && event.getX() <= right &&                  
                     event.getY() <= bottom && event.getY() >= top
                    ) {
                    // Do what you want to do here...
                    onEditClicked(viewHolder.getAdapterPosition());
                }
                return false;
            });

         } else {
             ...
         }
     }
}