i created new layout containing gridview. when application run, gridview getting expected content.
each cell of gridview has normal specific template. when clicked cell, new template loaded cell. with of @wanglugao, every thing working fine far.
now want refresh unclicked items normal template when clicked item. clicked item need include new template. but, when clicked item, remained new template if clicked other item.
image link illustrates current status
this baseadapter,
public class kategoriadapter extends baseadapter{ private context mcontext; private string[] categoryvalues; private bitmap[] pictures; private string mtag = "normal_template"; //indicate position using new template private int mnewtemplatepos = -1; //indicate normal template view private final string normal_template = "normal_template"; //indicate new template view private final string new_template = "new_template"; public kategoriadapter(context context, string[] categoryvalues, bitmap[] pictures) { this.mcontext = context; this.categoryvalues = categoryvalues; this.pictures = pictures; } //apply new template positon public void usenewtemplate(int pos) { mnewtemplatepos =pos; //notiy list data has changed , list refresh ui itself. notifydatasetchanged(); } @override public int getcount() { return categoryvalues.length; } @override public object getitem(int possition) { return null; } @override public long getitemid(int possition) { return 0; } @override public view getview(int possition, view convertview, viewgroup parent) { final layoutinflater inflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); if (convertview == null) { if (mnewtemplatepos==possition) { convertview = getnewtemplate(inflater, possition); //use tag indicate type of template convertview.settag(new_template); } else { convertview = getnormaltemplate(inflater, possition); convertview.settag(normal_template); mtag = (string) convertview.gettag(); } } else { switch (mtag) { case normal_template: //convertview normal template view need new template view in possition if (mnewtemplatepos==possition) convertview = getnewtemplate(inflater, possition); break; case new_template: //convertview new template view need normal template view in possition if (mnewtemplatepos!=possition) convertview = getnormaltemplate(inflater, possition); break; default: break; } } return convertview; } private view getnormaltemplate(layoutinflater inflater, int possition) { final view grid = inflater.inflate(r.layout.kategoriler_list_item, null); textview cname = (textview) grid.findviewbyid(r.id.grid_item_ad); imageview categorypictures = (imageview) grid.findviewbyid(r.id.grid_item_resim); cname.settext(categoryvalues[possition]); categorypictures.setimagebitmap(pictures[possition]); return grid; } private view getnewtemplate(layoutinflater inflater, int possition) { final view grid = inflater.inflate(r.layout.kategori_secenek_template, null); textview cname = (textview) grid.findviewbyid(r.id.grid_item_ad); cname.settext(categoryvalues[possition]); button btn_nesne_tani = (button) grid.findviewbyid(r.id.btn_nesneleri_taniyalim); button btn_cumle_kur = (button) grid.findviewbyid(r.id.btn_cumle_kuralim); btn_nesne_tani.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mcontext,"nesne",toast.length_short).show(); } }); btn_cumle_kur.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mcontext,"cümle",toast.length_short).show(); } }); return grid; }
}
and related part of activity.java
} final kategoriadapter adapter = new kategoriadapter(getapplicationcontext(), mkategoriler, kategoriresimleri); grid=(gridview)findviewbyid(r.id.gv_kategoriler); grid.setadapter(adapter); grid.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { adapter.usenewtemplate(position); toast.maketext(getapplicationcontext(), mkategoriler[position].tostring(),toast.length_short).show(); } });
thanks in advance.
the problem use of mtag
. use , determine template use when convert view not null, set when inflating new convert view.
mtag
should not instance field. should local (always) set in getview()
method. this,
tag tag = mnewtemplatepos==possition ? new : normal; switch (tag) { // inflate base on tag }
also, aren't using convert view properly; throw away in cases. if have 2 different cells in grid, should either use view types (google it) or have 1 layout , vary hiding or showing sections.
Comments
Post a Comment