Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ItemTouchHelper, Swipe to delete with text in canvas

We are implementing ItemTouchHelper in a RecyclerView, we are making a Override of onChildDraw when we swipe to the left in the RV, the problem is, if I have a RecyclerView with X size, and I try to dismiss the 2, 3, 4 or 5 elements, the only one that show the text is the first element in the List, but, if is an image it work pretty well

@Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        if (viewHolder.getAdapterPosition() == -1)
            return;
        View itemView = viewHolder.itemView;
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(40);
        paint.setTextAlign(Paint.Align.CENTER);
        String inbox = itemView.getContext().getResources().getString(R.string.sendinbox);
        c.drawText(inbox, itemView.getRight() - 200, itemView.getHeight()/2, paint);
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
like image 605
Dinorah Tovar Avatar asked Mar 08 '23 17:03

Dinorah Tovar


1 Answers

It's probably too late but I'll answer anyways. Your problem was when drawing Text on Canvas, you have to set X and Y position, and as the canvas is the same for all the items, you were constantly setting the same text at the same Y position. The solution was to add itemView.getTop() to your Text's Y position:

c.drawText(inbox, itemView.getRight() - 200, itemView.getTop() + itemView.getHeight() / 2, paint);

Sorry my english

like image 163
Francisco Tamburri Avatar answered Mar 25 '23 01:03

Francisco Tamburri