android - Appropriate use BaseAdapter class specifically getView method -
so far have seen several examples of applications use baseadapter
, arrayadapter<?>
. still not clear reasons why should way.
the first example extending arrayadapter<?>
, example used in listview, following getview method
@override public view getview(int position, view convertview, viewgroup parent) { view row = convertview; holder holder = null; // holder represents elements of view use // here initialized if(null == row) { row = layoutinflater.from(mcontext).inflate(layout_item_id, parent, false); holder = new holder(); holder.titletextview = (textview)row.findviewbyid(android.r.id.title); row.settag(holder); } else { holder = (holder) row.gettag(); } // here operations in holder variable example holder.titletextview.settext("title " + position); return row; } public static class holder { textview titletextview; }
now in second example found used baseadapter
on gridview getview method
// create new imageview each item referenced adapter public view getview(int position, view convertview, viewgroup parent) { imageview imageview; if (convertview == null) { // if it's not recycled, initialize attributes imageview = new imageview(mcontext); imageview.setlayoutparams(new gridview.layoutparams(85, 85)); imageview.setscaletype(imageview.scaletype.center_crop); imageview.setpadding(8, 8, 8, 8); } else { imageview = (imageview) convertview; } imageview.setimageresource(mthumbids[position]); return imageview; }
my question is: proper use of adapter necessary use "holder" static class, implications have on application performance , compatibility on multiple devices (min api 8).
it's not necessary use holder class; it's more important make sure re-use convertview whenever possible has noticeable speed improvement. being said, using holder offer better performance, if displaying lot of items, getview won't have inflate xml every time.
this video explains in greater detail: http://www.youtube.com/watch?v=wdbm6wveo70
Comments
Post a Comment