android - How to keep the status of the ViewHolder in RecyclerView -
i have recyclerview includes few grid cells. there textview above click me button. i'll show example, name @ textview when button clicked. problem me text inside textview not persistent.
here code in mainactivity:
private void initializedata(){ persons = new arraylist<>(); persons.add(new person("name 1", "23 years old", r.drawable.a)); persons.add(new person("name 2", "25 years old", r.drawable.b)); persons.add(new person("name 3", "35 years old", r.drawable.c)); persons.add(new person("name 4", "35 years old", r.drawable.c)); persons.add(new person("name 5", "35 years old", r.drawable.c)); persons.add(new person("name 6", "35 years old", r.drawable.c)); persons.add(new person("name 7", "35 years old", r.drawable.c)); persons.add(new person("name 8", "35 years old", r.drawable.c)); persons.add(new person("name 9", "35 years old", r.drawable.c)); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); recyclerview = (recyclerview)findviewbyid(r.id.my_recycler_view); recyclerview.sethasfixedsize(true); layoutmanager = new gridlayoutmanager(this, 1, linearlayoutmanager.horizontal, false); recyclerview.setlayoutmanager(layoutmanager); recyclerview.setitemanimator(new defaultitemanimator()); persons = new arraylist<>(); initializedata(); rvadapter adapter = new rvadapter(persons); recyclerview.setadapter(adapter); adapter.notifydatasetchanged(); }
code in adapter:
public class rvadapter extends recyclerview.adapter<rvadapter.personviewholder>{ list<person> persons; rvadapter(list<person> persons){ this.persons = persons; } @override public personviewholder oncreateviewholder(viewgroup viewgroup, int i) { view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.cards_layout, viewgroup, false); personviewholder pvh = new personviewholder(v); return pvh; } @override public void onbindviewholder(personviewholder personviewholder, int i) { personviewholder.personname.settext(persons.get(i).name); personviewholder.personage.settext(persons.get(i).age); personviewholder.personphoto.setimageresource(persons.get(i).photoid); final personviewholder vh = personviewholder; personviewholder.button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { vh.textview.settext(vh.personname.gettext().tostring()); } }); } @override public void onattachedtorecyclerview(recyclerview recyclerview) { super.onattachedtorecyclerview(recyclerview); } @override public int getitemcount() { return persons.size(); } public static class personviewholder extends recyclerview.viewholder { cardview cv; public textview personname; public textview personage; imageview personphoto; public textview textview; public button button; personviewholder(view itemview) { super(itemview); cv = (cardview)itemview.findviewbyid(r.id.cv); personname = (textview)itemview.findviewbyid(r.id.person_name); personage = (textview)itemview.findviewbyid(r.id.person_age); personphoto = (imageview)itemview.findviewbyid(r.id.person_photo); textview = (textview)itemview.findviewbyid(r.id.text); button = (button)itemview.findviewbyid(r.id.button); } } }
i think reason viewholder recreated. how deal kind of situation? thanks.
you can add boolean isshowname
model person
, in onbindviewholder()
method:
... personviewholder.button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { vh.textview.settext(vh.personname.gettext().tostring()); persons.get(i).setisshowname(true); // don't forget! } }); if (persons.get(i).isshowname()) { vh.textview.settext(vh.personname.gettext().tostring()); } else { vh.textview.settext(""); }
Comments
Post a Comment