Android annotation-bound control BindView

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/u012975705/article/details/49637401
Android annotation-bound control BindView

Bindview.java
Packagecom. Practice. Noyet. Coolweather. Util;Import Java. Lang. Annotation. ElementType;Import Java. Lang. Annotation. Retention;Import Java. Lang. Annotation. Retentionpolicy;Import Java. Lang. Annotation. Target;@Target (ElementType. FIELD) @Retention (Retentionpolicy. RUNTIME) public @interface BindView {int id ();Boolean click () Default False;}
Annotateutil.java
 PackageCom.practice.noyet.coolweather.util;ImportAndroid.annotation.TargetApi;Importandroid.app.Activity;ImportAndroid.app.Fragment;ImportAndroid.content.Context;ImportAndroid.util.Log;ImportAndroid.view.View;ImportJava.lang.reflect.Field; Public  class annotateutil {     Public Annotateutil() {    } Public Static void Initbindview(Object Currentclass, View sourceview) {field[] fields = Currentclass.getclass (). Getdeclaredfields ();if(Fields! =NULL&& fields.length >0) {field[] var6 = fields;intVAR5 = Fields.length; for(intVAR4 =0; VAR4 < VAR5;                ++VAR4) {Field field = VAR6[VAR4]; BindView BindView = (bindview) field.getannotation (Bindview.class);if(BindView! =NULL) {intViewId = Bindview.id ();BooleanClicklis = Bindview.click ();Try{field.setaccessible (true);if(Clicklis)                        {Sourceview.findviewbyid (viewId). Setonclicklistener ((View.onclicklistener) currentclass);                    } field.set (Currentclass, Sourceview.findviewbyid (viewId)); }Catch(Exception Var11)                    {Var11.printstacktrace (); }                }            }        }    } Public Static void Initbindview(Activity Aty)    {Initbindview (Aty, Aty.getwindow (). Getdecorview ()); } Public Static void Initbindview(View view) {Context CXT = View.getcontext ();if(CXTinstanceofActivity) {Initbindview (activity) CXT); }Else{LOG.D ("Annotateutil.java","The View don\ ' t has root view"); }    }@TargetApi( One) Public Static void Initbindview(Fragment Frag)    {Initbindview (Frag, Frag.getactivity (). GetWindow (). Getdecorview ()); }}
How to use

Activity, Fragment:

@BindViewprivate@OverrideprotectedvoidonCreate(Bundle savedInstanceState){     super.onCreate(savedInstanceState);     setContentView(R.layout.acitvity_weather);     AnnotateUtil.initBindView(this); }

In the Viewholder of adapter:

Packagecom. Practice. Noyet. Coolweather. Adapter;Import Android. Content. Context;Import Android. Graphics. Color;Import Android. View. View;Import Android. View. ViewGroup;Import Android. Widgets. TextView;Importcom. Practice. Noyet. Coolweather. Model. County;Importcom. Practice. Noyet. Coolweather. Util. Annotateutil;Importcom. Practice. Noyet. Coolweather. Util. BindView;public class Spinnerarrayadapter extends spinneradapter<county> {public Spinnerarrayadapter (context context) { Super (context);} @Override public View getView (int position, view view, ViewGroup viewgroup) {Holder Holder;if (view = = null) {view = Minflater. Inflate(Android. R. Layout. Simple_list_item_1, NULL);Holder = new Holder (view);View. Settag(holder);} else {holder = (Holder) view. Gettag();} County County = (county) GetItem (position);Holder. TextView. SetText(County. Getcountyname());Return view;} private class Holder {@BindView (id = Android. R. ID. Text1) Public TextView TextView;Public Holder (view view) {Annotateutil. Initbindview(This, view);}    }}

Spinneradapter See: http://blog.csdn.net/u012975705/article/details/48471835

How to use a custom view
initBindView(view);

Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source. Blog home: http://blog.csdn.net/u012975705

Android annotation-bound control BindView

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.