Android注解式绑定控件BindView

本文介绍了一种在Android开发中使用注解实现视图绑定的方法。通过自定义注解@BindView并配合AnnotateUtil工具类,可以简化findViewById的过程,并支持点击监听器的设置。适用于Activity、Fragment及Adapter等场景。
摘要由CSDN通过智能技术生成
Android注解式绑定控件BindView

BindView.java

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

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中:

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);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值