android - RecyclerView in RecyclerView parent.notifyDataSetChanged loose position of child RecyclerView -
i develop fragment using horizontal recyclerview in vertical recyclerview (like google play store).
when notifydatasetchanged parent recyclerview, child recyclerview lose position because setadapter call in onbindviewholder think. when scroll 5th position in first horizontal recyclerview if scroll down , come loose 5th position.
i try use recyclerview.scrolltoposition() don't work.
so think have 2 solution :
a way (by method or setting) keep position of child recycler view. (best solution)
a way set manually recyclerview position before refresh. (else solution)
here parent adapter :
public class profilescardviewlistadapter extends recyclerview.adapter<profilescardviewlistadapter.itemrowholder> { private arraylist<aidodysprofile> sectionslist; private context context; private boolean[] isshown; private profilecardviewitemadapter itemadapters[]; public profilescardviewlistadapter(arraylist<aidodysprofile> sectionslist, context context) { this.sectionslist = sectionslist; this.context = context; this.isshown = new boolean[sectionslist.size()]; this.itemadapters = new profilecardviewitemadapter[sectionslist.size()]; (int = 0; < sectionslist.size(); i++) { this.isshown[i] = true; this.itemadapters[i] = new profilecardviewitemadapter(sectionslist.get(i).getprofiles(), context, this); } } @override public itemrowholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.list_card_view_horizontal, null); final itemrowholder rowholder = new itemrowholder(view); return rowholder; } @override public void onbindviewholder(final itemrowholder holder, final int position) { string sectionname = sectionslist.get(position).getname(); aidodysprofile[] sectionitems = sectionslist.get(position).getprofiles(); holder.sectiontitle.settextsize(context.getresources().getdimension(r.dimen.text_size_profileslist_section_title)); holder.sectionbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { showhidesection(holder, position); } }); if (isshown[position]) { holder.itemrecyclerview.setvisibility(view.visible); holder.sectionbutton.settext(context.getstring(r.string.action_profiles_section_hide)); } else { holder.itemrecyclerview.setvisibility(view.gone); holder.sectionbutton.settext(context.gettext(r.string.action_profiles_section_show)); } if (!sectionslist.get(position).isleaf()) { // folder if (sectionname.equals("")) { holder.sectiontitle.setvisibility(view.gone); holder.sectionbutton.setvisibility(view.gone); } else { holder.sectiontitle.setvisibility(view.visible); holder.sectionbutton.setvisibility(view.visible); } holder.sectiontitle.settext(sectionname); } else { // profile return; } holder.itemrecyclerview.sethasfixedsize(true); holder.itemrecyclerview.setlayoutmanager(new linearlayoutmanager(context, linearlayoutmanager.horizontal, false)); holder.itemrecyclerview.setadapter(itemadapters[position]); } private void showhidesection(itemrowholder holder, int position) { if (holder.itemrecyclerview.getvisibility() == view.visible) { isshown[position] = false; holder.itemrecyclerview.setvisibility(view.gone); holder.sectionbutton.settext(context.gettext(r.string.action_profiles_section_show)); } else { isshown[position] = true; holder.itemrecyclerview.setvisibility(view.visible); holder.sectionbutton.settext(context.getstring(r.string.action_profiles_section_hide)); } } @override public int getitemcount() { return (sectionslist != null ? sectionslist.size() : 0); } public class itemrowholder extends recyclerview.viewholder { protected textview sectiontitle; protected recyclerview itemrecyclerview; protected button sectionbutton; public itemrowholder(view view) { super(view); this.sectiontitle = (textview) view.findviewbyid(r.id.section_title); this.itemrecyclerview = (recyclerview) view.findviewbyid(r.id.item_recycler_view); this.sectionbutton = (button) view.findviewbyid(r.id.section_button); } } }
child adapter :
class profilecardviewitemadapter extends recyclerview.adapter<profilecardviewitemadapter.singleitemrowholder> { private aidodysprofile[] itemslist; private currentuser currentuser; private context context; private int selectedpos = -1; private profilescardviewlistadapter parent; public profilecardviewitemadapter(aidodysprofile[] itemslist, context context, profilescardviewlistadapter parent) { this.itemslist = itemslist; this.context = context; this.parent = parent; this.currentuser = currentuser.getinstance(context); } @override public singleitemrowholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.list_item_card_view_horizontal, null); singleitemrowholder rowholder = new singleitemrowholder(view); return (rowholder); } @override public void onbindviewholder(final singleitemrowholder holder, final int position) { aidodysprofile profile = itemslist[position]; holder.itemcardview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { selectprofile(holder, position); } }); if (profile.equals(currentuser.getprofile())) { selectedpos = position; if (build.version.sdk_int >= build.version_codes.m) { holder.itemcardview.setcardbackgroundcolor(context.getcolor(r.color.aidodysred)); } else { holder.itemcardview.setcardbackgroundcolor(context.getresources().getcolor(r.color.aidodysred)); } } else { if (build.version.sdk_int >= build.version_codes.m) { holder.itemcardview.setcardbackgroundcolor(context.getcolor(r.color.white)); } else { holder.itemcardview.setcardbackgroundcolor(context.getresources().getcolor(r.color.white)); } } holder.itemtitle.settext(profile.getname()); if (build.version.sdk_int >= build.version_codes.lollipop) { holder.itempicture.setimagedrawable(context.getdrawable(r.drawable.ic_sheet_smile_black_48dp)); holder.button1.setimagedrawable(context.getdrawable(r.drawable.ic_edit_black_24dp)); holder.button2.setimagedrawable(context.getdrawable(r.drawable.ic_look_profile)); if (build.version.sdk_int >= build.version_codes.m) { holder.itemtitle.settextappearance(r.style.aidodys_text_profileslist_item); holder.itempicture.setcolorfilter(context.getcolor(r.color.white)); holder.topparts.setbackgroundcolor(context.getcolor(r.color.aidodysred)); } else { holder.itemtitle.settextcolor(context.getresources().getcolor(r.color.white)); holder.itemtitle.settextsize(context.getresources().getdimension(r.dimen.text_size_profileslist_item)); holder.itempicture.setcolorfilter(context.getresources().getcolor(r.color.white)); holder.topparts.setbackgroundcolor(context.getresources().getcolor(r.color.aidodysred)); } } else { holder.button1.setimagedrawable(context.getresources().getdrawable(r.drawable.ic_edit_black_24dp)); holder.button2.setimagedrawable(context.getresources().getdrawable(r.drawable.ic_look_profile)); holder.itemtitle.settextcolor(context.getresources().getcolor(r.color.white)); holder.itemtitle.settextsize(context.getresources().getdimension(r.dimen.text_size_profileslist_item)); holder.itempicture.setimagedrawable(context.getresources().getdrawable(r.drawable.ic_sheet_smile_black_48dp)); holder.itempicture.setcolorfilter(context.getresources().getcolor(r.color.white)); holder.topparts.setbackgroundcolor(context.getresources().getcolor(r.color.aidodysred)); } } private void selectprofile(singleitemrowholder holder, int position) { sharedpreferences.editor editor = context.getsharedpreferences("aidodys", 0).edit(); editor.putstring("profile", new gson().tojson(itemslist[position])); editor.apply(); currentuser.setprofile(itemslist[position]); parent.notifydatasetchanged(); notifydatasetchanged(); ((recyclerview)holder.itemcardview.getparent()).scrolltoposition(position); selectedpos = position; } @override public int getitemcount() { return (itemslist != null ? itemslist.length : 0); } public class singleitemrowholder extends recyclerview.viewholder { protected textview itemtitle; protected imageview itempicture; protected cardview itemcardview; protected imageview button1; protected imageview button2; protected linearlayout topparts; public singleitemrowholder(view view) { super(view); this.itemtitle = (textview)view.findviewbyid(r.id.item_title); this.itempicture = (imageview)view.findviewbyid(r.id.item_picture); this.itemcardview = (cardview)view.findviewbyid(r.id.card_view_list_item); this.topparts = (linearlayout)view.findviewbyid(r.id.card_view_list_item_top_part); this.button1 = (imageview)view.findviewbyid(r.id.item_button_1); this.button2 = (imageview)view.findviewbyid(r.id.item_button_2); } } }
if has solution me
thank you
store x scroll offset in sparsearray position , restore when bind view holder.
public class profilescardviewlistadapter extends recyclerview.adapter<profilescardviewlistadapter.itemrowholder> { private sparseintarray sparsearray = new sparseintarray(); @override public void onbindviewholder(final itemrowholder holder, final int position) { // use srollby animate scrolling holder.itemrecyclerview.srollby(sparsearray.get(position, 0), 0); // or scrollto restore previous x offset //holder.itemrecyclerview.srollto(sparsearray.get(position, 0), 0); holder.itemrecyclerview.setonscrolllistener(new recyclerview.onscrolllistener() { @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { sparsearray.put(position, dx); } } } }
Comments
Post a Comment