Android xutils FRAME (ii) viewutils

Source: Internet
Author: User

frame address: https://github.com/wyouflf/xUtilsfunction:
    • UI binding and event binding can be done in a fully annotated manner.
    • No need for Findviewbyid and Setclicklistener.

UI bindings:

@ContentView (r.layout. View_acti )//setcontentview annotations above the activity statement

@ViewInject(r.id. lv_test )// Reflection View annotations on a view declaration

@ResInject (id=r.string. app_name, Type=restype. String) //Resource Reflection ResType defines some of the resource types under Res/values

@PreferenceInject ("key")//annotation on a Preference component value for its key

Viewutils.inject (activity);//through reflection, find the corresponding view, and initialize


Event Bindings

@OnClick (r.id. Btn_test)//Bind a custom method to a view event

The package com.lidroid.xutils.view.annotation, which defines some common events, has a characteristic that there is only one internal implementation method


Process Analysis for event binding and execution
        @ViewInject (r.id.btn_test) private Button Mbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {Supe R.oncreate (savedinstancestate); Viewutils.inject (this); Mbutton.settext (label); Method testbuttonclickmethod;try {testbuttonclickmethod = This.getclass (). GetMethod ("Testbuttonclick", View.class); Myonclick OnClick = testbuttonclickmethod.getannotation (Myonclick.class);        Myeventbase eventbase = Onclick.annotationtype (). Getannotation (Myeventbase.class); class<?> Listenertype = Eventbase.mlistenertype ();//view.onclicklistener.class String listenerSetter = EventB Ase.mlistenersetter ();//setonclicklistener String methodName = Eventbase.mmethodname ();//onclick MyD        Ynamichandler Dynamichandler = new Mydynamichandler (viewactivity.this); Dynamichandler.addmethod (MethodName, Testbuttonclickmethod);//Add the actual method to be called to the handler inside the map set Object listener = Pro      Xy.newproxyinstance (//Dynamic Agent Listenertype.getclassloader (),          New Class<?>[]{listenertype}, Dynamichandler); Method Seteventlistenermethod = Mbutton.getclass (). GetMethod (Listenersetter, listenertype);//         Setonclicklistener-method/* * onCreate () directly executed: * The first call here is: Mbutton.setonclicklistener (listener); * Listener into proxy object * Note: Dynamichandler's Invoke * * Touch: * dispatchtouchevent After Action-down and action-up, the onclick-event *onclicklistener that triggered the view has been set to the proxy object listener the * will call listener corresponding processor Dynami Chandler.invoke () *invoke: Final Execution Testbuttonclick Method * */Seteventlistenermethod.invoke (Mbutto         n, listener);        } catch (Exception e) {e.printstacktrace ();} } @MyOnClick (r.id.btn_test) public void Testbuttonclick (View v) {logutil.print ("clicked Button");} @Target (Elementtype.annotation_type) @Retention (retentionpolicy.runtime) public @interface myeventbase {class<?    > Mlistenertype (); String MlistenersettER (); String mmethodname ();} @Target (Elementtype.method) @Retention (retentionpolicy.runtime) @MyEventBase (Mlistenertype = View.OnClickListener.class, Mlistenersetter = "Setonclicklistener", Mmethodname = "OnClick") private static @ Interface Myonclick {int[] value (); int[] ParentID () default 0;} public class Mydynamichandler implements Invocationhandler {private weakreference<object> handlerref;private Final hashmap<string, method> methodmap = new hashmap<string, method> (1);p ublic Mydynamichandler (Object Handler) {this.handlerref = new weakreference<object> (handler);} public void Addmethod (String name, method method) {Methodmap.put (name, method);} Public Object GetHandler () {return handlerref.get ();} public void SetHandler (Object handler) {this.handlerref = new weakreference<object> (handler);} @Overridepublic object Invoke (Object proxy, Method method, object[] args) throws Throwable {Object handler = Handlerref.get ();//viewactivityif (handleR! = null) {String methodName = Method.getname (); System.out.println ("Invocationhandler.invoke--methodname:" + methodName); method = Methodmap.get (MethodName); if ( Method! = null) {return Method.invoke (handler, args);}} return null;}} @Overridepublic boolean dispatchtouchevent (motionevent ev) {System.out.println ("dispatch"); return Super.dispatchtouchevent (EV);}

Similarly, other events will bind you to the methods and corresponding events that use the event annotations in the form of dynamic proxies as above.


Android xutils FRAME (ii) viewutils

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.