java - RecyclerView: IndexOutOfBoundsException. Invalid item position -
i have 2 fragments: a & b.
fragment b contains searchview , recyclerview.
adapter = new searchlistadapter(items); recyclerview = new recyclerview(getcontext()); recyclerview.sethasfixedsize(true); recyclerview.setadapter(adapter); recyclerview.setlayoutmanager(new linearlayoutmanager(getcontext())); params = new relativelayout.layoutparams(layoutparams.match_parent, layoutparams.match_parent); params.addrule(below, search_view_id); addview(recyclerview, params); @override public boolean onquerytextchange(string newtext) { arraylist<mymodel> filteredmodellist = adapter.filter(newtext); recyclerview.scrolltoposition(0); return true; }
implementing filtering taken away: how filter recyclerview searchview
problem
1) @ beginning on fragment a, go fragment b administered in searchview more characters , filtered list (for example consisting of single element).
2) next, through getfragmentmanager().popbackstack(); return fragment a.
3) @ same time, in fragment b in method of onstop(); clean searchview:
@override public void onstop() { if(searchview != null) { searchview.setquery("", false); } }
4) next, return fragment b , error:
java.lang.indexoutofboundsexception: inconsistency detected. invalid item position 1(offset:35).state:35
i've been looking @ problem , found following:
a) issue 77846
b) issue 77232
c) habra
d) recyclerview layoutmanager part 1 (generatedefaultlayoutparams)
e) indexoutofboundsexception invalid item position xx(xx). item count:xx
he came conclusion bug recyclerview , has not yet been fixed.
as result, necessary implement filtering without animation:
public void filter(string chartext) { chartext = chartext.tolowercase(); mitems = new arraylist<mymodel>(); if (chartext.length() == 0) { mitems.addall(moriginalitems); } else { (mymodel model : moriginalitems) { string textid = model.getid().tolowercase(); if (textid.contains(chartext)) { mitems.add(model); } } } notifydatasetchanged(); }
is true, , there error in code?
thank you!
update
activity:
public class myactivity extends appcompatactivity
simplefragmentmanager:
private static void moveto(basefragment fragment) { if(fragment != null) { fragmentmanager fragmentmanager = _activity.getfragmentmanager(); fragmenttransaction ft = fragmentmanager.begintransaction(); basefragment currentfragment = getcurrentfragment(); if(fragment != currentfragment) { ft.replace(_containerid, fragment, fragment.gettype()); if (currentfragment != null) ft.addtobackstack(null); ft.commit(); } } }
fragment & b:
@nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { if(view == null) { view = new alayout(container.getcontext()); //if fragment b > view = new blayout(container.getcontext()); view.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.match_parent, relativelayout.layoutparams.match_parent)); } return view; }
alayout & blayout extends relativelayout
blayout searchview , recycleview showed above.
important notice:
1) if comment out line: recyclerview.scrolltoposition(0);
- no bug! (and no scroll top :) )
2) more precise conditions obtaining bug:
a) go fragment_1 track on fragment_2;
b) enter query in search (string present: recyclerview.scrolltoposition(0));
c) return fragment_1 (this time search query clean - no errors!);
d) again go fragment_2.
>>we error!
a) go fragment_1 track on fragment_2;
b) do not enter in search (string present: recyclerview.scrolltoposition (0));
c) go fragment_1. (this time search query clean - no errors!);
d) once again going fragment_2.
>>no error!
Comments
Post a Comment