android - What is the working of setTag and getTag in ViewHolder pattern? -
i have simple code snippet implementing custom listview.
my code follows:
weatheradapter.java :
public class weatheradapter extends arrayadapter<weather>{ context mcontext; int mlayoutresourceid; weather mdata[] = null; view row; public weatheradapter(context context, int layoutresourceid, weather[] data) { super(context, layoutresourceid, data); mlayoutresourceid = layoutresourceid; mcontext = context; mdata = data; } @override public view getview(int position, view convertview, viewgroup parent) { row = convertview; weatherholder holder = null; if(row == null) { layoutinflater inflater = ( (activity) mcontext).getlayoutinflater(); row = inflater.inflate(mlayoutresourceid, parent, false); holder = new weatherholder(row); row.settag(holder); } else { holder = (weatherholder)row.gettag(); } weather w = mdata[position]; holder.txttitle.settext(w.mtitle); holder.imgicon.setimageresource(w.micon); return row; }
weatherholder.java:
class weatherholder { imageview imgicon; textview txttitle; public weatherholder(view v){ imgicon = (imageview)row.findviewbyid(r.id.imgicon); txttitle = (textview)row.findviewbyid(r.id.txttitle); } } }
i have seen many answers on , other sites , understood recycling mechanism of listview.
i understood viewholder, can hold child views in adapter , not have call findviewbyid()
many times. so, optimization.
but have confusion in settag(holder)
, gettag()
methods. from question, came know making key-value pair on multiple objects, can access them easily. but, not understand why required here...because, not have multiple holder objects...only have change holder's variables each time. can code here without using settag
, gettag
?
can explain better settag
, gettag
"here"?
tag
mechanism make views
remember something, object
integer
string
or like.
so when listview
going create first time convertview
null
. create new convertview
, put of references
of objects
of row
in viewholder
. save viewholder
memory of convertview
(settag). android
takes convertview
, puts in pool
recycle
, passes
again you. pool
may not have enough convertviews
again passes new convertview
thats null
. again story repeated till pool
of android
filled up. after android
takes convertview
pool , passes you. find not null
ask object references
gave first time? (gettag) , whatever like.
more elaboration on below line
but pool may not have enough convertviews again passes new convertview thats null
android pool
empty when listview
going create. first item of listview
sends convertview
must displayed. after android
saves in pool
, pool
contains 1 convertview
. second item of listview
going create android can not use pool because has 1 element , element first item , being shown right has pass convertview
. process repeates until android
found convertview
in pool
thats not being displayed , passes you.
android inflates each row till screen filled after when scroll list uses holder.
Comments
Post a Comment