0

While putting in a custom view from this here, I noticed a few problems.

  • As the item does not clear, when moved out of the screen, it leaves a gaping hole in its stead.
  • When swiped but not completely, the alarm icon is still visible on screen.

Here is a screenshot of my woes: bugs bugs everywhere

ItemTouchHelperCallback:

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    View itemView = viewHolder.itemView;
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        itemView = viewHolder.itemView;
        float height = (float) itemView.getBottom() - (float) itemView.getTop();
        float width = height / 3;

        // First, a rectangle is drawn on Canvas which covers the whole item view in preferred color.
        // Then, we draw a Bitmap icon over that in specified location.
        // Swiped right, green background with tick icon
        // Swiped left, Orange background with alarm clock.
        if(dX > 0){
            // On the left side i.e swiped right
            p.setColor(ContextCompat.getColor(context, R.color.green));

            // Draw Rect with varying right side, equal to displacement dX
            RectF background = new RectF((float) itemView.getLeft(), (float) itemView.getTop(), dX,(float) itemView.getBottom());
            c.drawRect(background,p);

            // Retrieves icon as Bitmap
            icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_tick);
            RectF icon_dest = new RectF((float) itemView.getLeft() + width ,(float) itemView.getTop() + width,(float) itemView.getLeft()+ 2*width,(float)itemView.getBottom() - width);

            // Set the image icon for Right swipe
            c.drawBitmap(icon,null,icon_dest,p);
        } else {
            // On the right side i.e swiped left
            p.setColor(ContextCompat.getColor(context, R.color.orange));

            // Draw Rect with varying left side, equal to the item's right side
            // plus negative displacement dX
            RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(),(float) itemView.getRight(), (float) itemView.getBottom());
            c.drawRect(background,p);

            // Retrieves icon as Bitmap
            icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_alarm_off);
            RectF icon_dest = new RectF((float) itemView.getRight() - 2*width ,(float) itemView.getTop() + width,(float) itemView.getRight() - width,(float)itemView.getBottom() - width);

            // Set the image icon for Left swipe
            c.drawBitmap(icon,null,icon_dest,p);
        }

        // Fade out the view as it is swiped out of the parent's bounds
        final float alpha = 1.0f - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
        viewHolder.itemView.setAlpha(alpha);
        viewHolder.itemView.setTranslationX(dX);
    }
    else 
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder,
                              int actionState) {
    // We only want the active item
    if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
        if (viewHolder instanceof ItemTouchHelperViewHolder) {
            ItemTouchHelperViewHolder itemViewHolder =
                    (ItemTouchHelperViewHolder) viewHolder;
            itemViewHolder.onItemSelected();
            viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
        }
    }

    super.onSelectedChanged(viewHolder, actionState);
}

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    // Called when RV item goes back into idle state i.e. finished drag or swipe
    super.clearView(recyclerView, viewHolder);
    if(viewHolder instanceof ItemTouchHelperViewHolder) {
        ItemTouchHelperViewHolder itemTouchHelperViewHolder = (ItemTouchHelperViewHolder) viewHolder;
        itemTouchHelperViewHolder.onItemClear();
        viewHolder.itemView.setBackgroundColor(0);
        icon.recycle();
    }
}

How would I:

  1. Make the Bitmap fade out so the items below update to fill in the gap

  2. Fix the icon and space problem

  3. Solve my problems and lower my blood pressure

I would be grateful for any solution you can provide.

UPDATE:

The way I fixed the icon problem was quite silly. In the else statement after if (dX > 0), I replaced with else if (dX < 0) and thus, the icon disappeared. My theory is that this is because when the RV item is replaced, dX is not > 0, and so the else statement is called where the icon is drawn again.

Community
  • 1
  • 1
planetastro
  • 1,716
  • 2
  • 15
  • 28

1 Answers1

0

The way I fixed the icon problem was quite silly. In the else statement after if (dX > 0), I replaced with else if (dX < 0) and thus, the icon disappeared. My theory is that this is because when the RV item is replaced, dX is not > 0, and so the else statement in onChildDraw is called where the icon is drawn again.

planetastro
  • 1,716
  • 2
  • 15
  • 28