java - RecyclerView item's image bug -


problem: glitch bug recyclerview's child's item imageview

i have recyclerview.each item has imageview id "like" empty star.

when click on item's "like", example, first, star changing empty filled in yellow, means item liked.

when click on first item ok, should be, @ same time, have bug, changing sixth item's "like" filled star, shouldn't done, item wasn't liked yet.

if click on second item's - seventh item have same bug.

to fill item's in viewholder have model - recipe.

open class recipe constructor(open var label: string,                            open var image: string,                            open var url: string,                            open var ingredients: realmlist<ingredient>?,                            open var calories: float,                            open var totalweight: float,                            open var id: int,                            open var isfavorite: boolean) : realmobject()  

so when clicking on item check whether isfavorite or not, , change "like" imageview accordingly.

have got ideas how should fix this?

i tried turn off recycling of items in adapter, unfortunately, in case, "liked" state isn't saved too.

onclick listener "like" button

itemview.like.onclick {                 if (recipe.isfavorite) {                     itemview.like.image = contextcompat.getdrawable(itemview.context, r.drawable.ic_star_before_like)                     recipe.isfavorite = false                     databaseservice.removerecipefromfavorites(recipe)                     itemview.context.toast("recipe removed favorites.")                 } else {                     itemview.like.image = contextcompat.getdrawable(itemview.context, r.drawable.ic_star_after_like)                     recipe.isfavorite = true                     databaseservice.addnewfavoriterecipe(recipe)                     itemview.context.toast("recipe added favorites.")                 }              } 

xml-file

<linearlayout   android:layout_width="match_parent"   android:layout_height="0dip"   android:layout_weight="0.8"   android:gravity="center|right"   android:orientation="horizontal">    <imageview     android:id="@+id/like"     android:layout_width="@dimen/icon_size_card"     android:layout_height="@dimen/icon_size_card"     android:padding="5dp"     android:layout_marginright="@dimen/icon_margin_card"     android:src="@drawable/ic_star_before_like" />    <imageview     android:id="@+id/share"     android:layout_width="@dimen/icon_size_card"     android:layout_height="@dimen/icon_size_card"     android:padding="5dp"     android:layout_marginright="@dimen/icon_margin_card"     android:src="@drawable/ic_share" />    <imageview     android:id="@+id/save"     android:layout_width="@dimen/icon_size_card"     android:layout_height="@dimen/icon_size_card"     android:padding="5dp"     android:layout_marginright="@dimen/icon_margin_card"     android:src="@drawable/ic_save_recipe" />    <imageview     android:id="@+id/expand"     android:layout_width="@dimen/icon_size_card"     android:layout_height="@dimen/icon_size_card"     android:padding="5dp"     android:layout_marginright="@dimen/icon_margin_card"     android:src="@drawable/ic_down_arrow" />  </linearlayout> 

recyclerview reuses viewholders creates oncreateviewholder. put simply, think of views going off screen on top coming around on bottom, , vica versa. therefore, nice way of solving want following:

  1. set icon right drawable in onbindviewholder method. way, whether viewholder being bound first time, or had drawable reflecting recipe's favourited state already, refreshed properly.

    without knowing adapter looks like, should give idea of how (the example code assumes have recipes in list called recipes):

    override fun onbindviewholder(holder: viewholder, position: int) {     val recipe = recipes[position]     val itemview = holder.itemview     itemview.like.image = contextcompat.getdrawable(itemview.context,             if(recipe.isfavorite) r.drawable.ic_star_before_like else r.drawable.ic_star_after_like) } 
  2. in listener, modify recipe same way you're doing now, instead of setting image directly, notify adapter recipe @ given position has changed (notifyitemchanged(position)), make re-bind item, refreshing icon reflect new state. again, don't know you're setting listener, assume know position recipe @ in list there.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -