android - Leak on ViewHolder and Recycler view with MVVM -
i'm having issue viewholder leaking reference context via simple toast
this adapter (dummy data)
public class changelanguageadapter extends recyclerview.adapter<languageviewholder> { @override public languageviewholder oncreateviewholder(viewgroup parent, int viewtype) { layoutinflater layoutinflater = layoutinflater.from(parent.getcontext()); itemlanguagebinding itembinding = itemlanguagebinding.inflate(layoutinflater, parent, false); return new languageviewholder(itembinding); } @override public void onbindviewholder(languageviewholder holder, int position) { string language = "dummy language"; holder.setlanguage(language); holder.bind(language); } @override public int getitemcount() { return 20; } }
this viewholder
public class languageviewholder extends recyclerview.viewholder { private itemlanguagebinding binding; private string language; public languageviewholder(itemlanguagebinding itemlanguagebinding) { super(itemlanguagebinding.getroot()); this.binding = itemlanguagebinding; } public void bind(string a){ setlanguage(a); binding.setviewmodel(this); binding.executependingbindings(); } public string getlanguage() { return language; } public void setlanguage(string language) { this.language = language; } public void onclick(view v){ toast.maketext(v.getcontext(), language, toast.length_long).show(); } }
and how binding done:
<?xml version="1.0" encoding="utf-8"?>
<data> <variable name="viewmodel" type=".adapters.languageviewholder" /> </data> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{viewmodel.language}" android:textcolor="@color/colorblack" android:onclick="@{viewmodel::onclick}" tools:text="language" />
should nullify binding when ondestroy executed? don't understand why viewmodel being leaked
Comments
Post a Comment