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?
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>
CodePudding user response:
Make sure to clear background mIV.setBackground(null);
in else statement.
CodePudding user response:
Your onBindViewHolder is broken. A properly written onBindViewHolder sets EVERY field of the view that could possibly change. And in a proper RecyclerView implementation, non of the click handlers or other actions ever touch the view itself, they all change the model then notify the adapter an iten has changed. Obviously you're not doing that- you're inheriting some of the styling from the old view. Fix your onBindViewHolder to specify all of the view information.
CodePudding user response:
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());
}