android - GridLayoutManager spanSizeLookup bug -
i having issue gridlayoutmanger
setspansizelookup
, when orientation of activity changes, changing span count while checking if specific position sort of type, works on orientation change, problem is, i'm using zxing
library barcode scanning, whenever button
clicked i'm opening zxing
default intent , retrieve date it, when zxing
opening goes landscape
, current activity orientation portrait
gives me illegalargumentexception
layout manage throws when opening xzing
intent, crash log this
java.lang.illegalargumentexception: item @ position 0 requires 2 spans gridlayoutmanager has 1 spans.
problem doesn't occur if rotate phone, happens when launch xzing
intent, couldn't figure out how how solve issue bugging me. here spansizelookup
-
manager.setspansizelookup(new gridlayoutmanager.spansizelookup() { @override public int getspansize(int position) { return adapter.getholders().get(position).getlabelholder() != null ? getresources().getinteger(r.integer.span) : 1; } });
the span
base on screen size can 1-2, 2-3 , 3-4. repeat doesn't give me error on orientation change, error occur when open zxing
p.s if open zxing intent
while activity on landspace
crash wont occur.
edit
it seems launching zxing
default intent causing issue, i'm having activity
goes landscape
after launches , exception
didn't occur, workaround did, created activity
handles barcode
scanning delay of 1 second launch intent
if didn't that, throw same exception
.
edit 2
i found that, without setspanspizelookup crash still occurs. calling manager.setspancount(getresources().getinteger(r.integer.span))
here's source code throws exception in gridlayoutmanager.layoutchunk()
final int spansize = getspansize(recycler, state, pos); if (spansize > mspancount) { throw new illegalargumentexception("item @ position " + pos + " requires " + spansize + " spans gridlayoutmanager has " + mspancount + " spans."); }
is spansize greater spancount? try debug , change spancount , see how works.
---- updated ----
there're couple ways switch between list , grid.
only changing spancount.
you have call
layoutmanager.requestlayout()
after change spancount. otherwise app may crash.only changing spansizelookup.
private class customspansize extends gridlayoutmanager.spansizelookup { private static final int list_span_size = 1; private static final int grid_span_size = 2; private int mspansize = list_span_size; public void aslist() { mspansize = list_span_size; } public void asgrid() { mspansize = grid_span_size; } @override public int getspansize(int position) { return mspansize; } }
and use that
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mlayoutmanager = new gridlayoutmanager(this, 2); mcustomspansize = new customspansize(); mlayoutmanager.setspansizelookup(mcustomspansize); recyclerview recyclerview = (recyclerview) findviewbyid(r.id.recycler); recyclerview.sethasfixedsize(true); recyclerview.setlayoutmanager(mlayoutmanager); ... } public void somefunction() { if (changed) { mcustomspansize.aslist(); } else { mcustomspansize.asgrid(); } // use update layout madapter.notifydatasetchanged(); // or can use update layout mlayoutmanager.requestlayout(); }
this way won't crash forget call
notifydatasetchanged
orrequestlayout
Comments
Post a Comment