android - Get resource id from which view was inflated -
is there way obtain resources id (like r.layout.viewtoinflate) of layout view inflated?
i trying implement list view uses 2 custom layouts list items. item layout used based on field in objects used populate list view.
what missing in custom adapter way know resource 'convertview' in getview() inflated from. if info can compare , determine whether can re-use convert view or if have replace appropriate layout current item.
so ideally this:
@override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; jsonobject curritem = mitems.getjsonobject(position); int layouttype; if (curritem.getboolean("alternate")) layouttype = r.layout.list_item_b; else layouttype = r.layout.list_item_a; if (convertview == null || <convertview.resourceid> != layouttype ) { convertview = inflater.inflate(layouttype, null); //finish setting new view , holder } else { //get view holder view reuse } //populate view required content return convertview; }
at worst not re-use views @ , inflate layout need every time, seems wasteful.
there official way use multiple layouts, override getviewtypecount()
, getitemviewtype()
. these methods inform adapter expect multiple layouts , convertview
appropriate layout (so don't need check resource id.) please see this example sample code.
but if want find out layout which, check unique characteristic. example can use findviewbyid()
on view exists in 1 layout, check whether returns null
or not.
Comments
Post a Comment