android - Custom views inside ListView with itemViewType -
i have listview in application. adapter listview contains multiple item view types (around 5 till now), via can inflate different types of row views inside listview.
all row views inflated inside adapter custom subclassed view/view group.
public class customview1 extends relativelayout { bundle bundle; public customview1(bundle bundle) { super(context); this.bundle = bundle; addsubviews(bundle.getbundlelist("list")); } private void addsubviews(arraylist<bundle> list) { for(bundle element : list) { //add sub views via reflection view view = (view) class.forname(packagename + type).getconstructor(bundle.class).newinstance(element); addview(view); } } //called getview() in adapter when convertview != null public void onrecycle(bundle bundle) { if(bundle != this.bundle) { this.bundle = bundle; removeallviews(); addsubviews(bundle.getbundlelist("list")); } } }
bundle passed each custom view contains layout info view. in way, can create , add view/viewgroup inside viewgroup. till now.
now problem comes when code runs inside listview. since view types created adapter initially, scrolling jerks lot because adapter keeps on creating new custom views of different itemviewtype. how reduce jerks in listview ? ideas? in listview, viewtypes different @ top 5 positions, adapter has create these views , makes experience sluggish.
even when adapter recycles similar view type convertviews after 5th index, clear container using removeallviews() , run loop again because subview bundle list of incoming bundle 6th position onwards might different. in end, adapter recycling empty viewgroups. since subview list can possibly contain (maybe 1 more bundle list inside element bundle), have removeallviews() accommodate new subview tree in recycled convertview.
i thought of using vertical scrollview take memory upfront, , number of custom views inflated dynamic, can increase 20.
the app running scroll bad there hardly usability left, looking till have achieved nothing adding dynamic behavior also. please suggest me ways counter problem.
i suspecting use of setlayoutparams inside customview classes may stopping scroll because set width/height of views after created.
update #1 getview() code using viewholder pattern
viewholder holder; if(convertview == null) { holder = new viewholder(); holder.customview1 = new customview1(bundle); convertview = holder.customview1; convertview.settag(holder); } else { holder = (viewholder)convertview.gettag(); } holder.customview1.onrecycle(bundle);
listview
has excellent support different view
types. make sure use view holder pattern avoid jerky scrolling , override
getviewtypecount()
, getitemviewtype()
.
more detail http://android.amberfog.com/?p=296
Comments
Post a Comment