javaandroidandroid-recyclerviewrefreshpresentation

how to refresh a RecyclerView draw (not datas)?


In a RecyclerView, I change the presentation for some items as follows:

        if (stk.getQty() == 0) {    // mise en valeur des stocks à 0 non purgeables
            cellStockQty.setTextColor(Color.RED);
            GradientDrawable border = new GradientDrawable();
            border.setColor(mIV.getResources().getColor(R.color.colorAugment));  // white background
            border.setStroke(1, mIV.getResources().getColor(R.color.colorAccent));  // black border with full
            mIV.setBackground(border);
        }

When I delete an item, I manage to remove it from the display, with notifyItemRemoved(index); but the next item takes the presentation of the item that was deleted.

Of course, if I leave this activity then come back, all is right.

How to make sure to refresh the display, depending on the data?

before deleting Jjjjjj item

after deleting Jjjjjj item

Edit:

Here is my original layout

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    android:foreground="@drawable/card_foreground"
    app:cardBackgroundColor="@color/stockRecyclerviewBackground"
    app:cardCornerRadius="6dp"
    app:cardElevation="4dp"
    app:cardMaxElevation="6dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="true">

    <include layout="@layout/recystock_cell_include" />

</androidx.cardview.widget.CardView>

Solution

  • It seems that just doing:

    mStockRecyclerView.setAdapter(mAdapter);
    

    is enough.

    Sorry for the noise.

    Another solution, found with the help of the answers here and there Reset Button to default background is to reset the background, using setTag() and getTag() like:

    if ( mIV.getTag() == null ) mIV.setTag(mIV.getBackground());
    if (cellStockQty.getTag() == null ) cellStockQty.setTag(cellStockQty.getCurrentTextColor());
    if (stk.getQty() == 0) {    // mise en valeur des stocks à 0 non purgeables
        cellStockQty.setTextColor(Color.RED);
        GradientDrawable border = new GradientDrawable();
        border.setColor(mIV.getResources().getColor(R.color.colorAugment));  // white background
        border.setStroke(1, mIV.getResources().getColor(R.color.colorAccent));  // black border with full
        mIV.setBackground(border);
    } else {
        cellStockQty.setTextColor((int)cellStockQty.getTag());
        mIV.setBackground((Drawable) mIV.getTag());
    }