onclicklistener - how can I fire Onclick event programmatically in android? -
i have custom view withe 2 lineat layouts : first view's header , second the details view.
in custom view, onclicklistener of header linearlayout defined: when fires, collapses/expandes second linearlayout.
what want add more functionalities header's onclicklistener event (ie : collapse/expand second layout , show toast).
i can't modify source code of custom view. tried set new onclicklistener hides initial event (collapse/expand).
how should implement this?
the source code of custom view:
public class expandolayout extends viewgroup { /* declarations */ private linearlayout header; private linearlayout footer; /* code */ @override protected void onfinishinflate() { header= new linearlayout(context); header.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { toggleexpand(); } }); } } what want add code defined onclicklistener event in activity. that:
public class myactivity extends activity { private linearlayout mycustomview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.rsdetail); mycustomview= (mycustomview) findviewbyid(r.id.expanded); mycustomview.getchildat(0).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if(v instanceof linearlayout) { v.performclick(); toast.maketext(getactivity(), "expandoonclicklistener", 2000).show(); } } }); }
simple solution original onclicklistener , fire in new one:
final onclicklistener predefinedlistener = mycustomview.getchildat(0).getonclicklistner(); mycustomview.getchildat(0).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if(v instanceof linearlayout) { predefinedlistener.onclick(v); // calls default (defined mycustomview) toast.maketext(getactivity(), "expandoonclicklistener", 2000).show(); } } }); sadly, view not have getonclicklistner(), guess can use reflection it. stored in field monclicklistener (source).
this how can onclicklistener defined layout:
onclicklistener tmponclicklistener = null; try { class<view> cls = (class<view>) class.forname("android.view.view"); field fld = cls.getdeclaredfield("monclicklistener"); fld.setaccessible(true); // because protected tmponclicklistener = (onclicklistener) fld.get(mycustomview.getchildat(0)); fld.setaccessible(false); // restore it's original property } catch (securityexception e) { e.printstacktrace(); } catch (nosuchfieldexception e) { e.printstacktrace(); } catch (illegalargumentexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } catch (classnotfoundexception e) { e.printstacktrace(); } final onclicklistener predefinedlistener = tmponclicklistener; if (predefinedlistener != null) { mycustomview.getchildat(0).setonclicklistener(new onclicklistener() { @override public void onclick(view paramview) { predefinedlistener.onclick(paramview); toast.maketext(getactivity(), "expandoonclicklistener", toast.length_long).show(); } }); i didn't bother handle exception correctly, it's enough idea. might messy, it's 5 lines of new code solve problem.
Comments
Post a Comment