Android註解式繫結控制項BindView

來源:互聯網
上載者:User

標籤:android   控制項   bindview   註解   綁定   

轉載請註明 出處:http://blog.csdn.net/u012975705/article/details/49637401
Android註解式繫結控制項BindView

BindView.java
package com.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
package com.practice.noyet.coolweather.util;import android.annotation.TargetApi;import android.app.Activity;import android.app.Fragment;import android.content.Context;import android.util.Log;import android.view.View;import java.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;            int var5 = fields.length;            for(int var4 = 0; var4 < var5; ++var4) {                Field field = var6[var4];                BindView bindView = (BindView)field.getAnnotation(BindView.class);                if(bindView != null) {                    int viewId = bindView.id();                    boolean clickLis = 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(cxt instanceof Activity) {            initBindView((Activity)cxt);        } else {            Log.d("AnnotateUtil.java", "the view don\‘t have root view");        }    }    @TargetApi(11)    public static void initBindView(Fragment frag) {        initBindView(frag, frag.getActivity().getWindow().getDecorView());    }}
使用方式

Activity、Fragment中:

 @BindView(id = R.id.headerlayout) private HeaderLayout headerLayout; @Override protected void onCreate(Bundle savedInstanceState){     super.onCreate(savedInstanceState);     setContentView(R.layout.acitvity_weather);     AnnotateUtil.initBindView(this); }

Adapter的ViewHolder中:

package com.practice.noyet.coolweather.adapter;import android.content.Context;import android.graphics.Color;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import com.practice.noyet.coolweather.model.County;import com.practice.noyet.coolweather.util.AnnotateUtil;import com.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詳見:http://blog.csdn.net/u012975705/article/details/48471835

對已自訂View的使用方式
initBindView(view);

著作權聲明:本文為博主原創文章,轉載請註明出處。部落格首頁:http://blog.csdn.net/u012975705

Android註解式繫結控制項BindView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.