@Target(ElementType.FIELD)//表示用在字段上
@Retention(RetentionPolicy.RUNTIME)//表示在生命周期是运行时
public @interface InjectView {
public int id() default 0;
public String click() default "";
public String longClick() default "";
public String itemClick() default "";
public String itemLongClick() default "";
}
public class EventListener implements OnClickListener, OnLongClickListener, OnItemClickListener,OnItemLongClickListener {
private Object handler;
private String clickMethod;
private String longClickMethod;
private String itemClickMethod;
private String itemLongClickMehtod;
public EventListener(Object handler) {
this.handler = handler;
}
public EventListener click(String method){
this.clickMethod = method;
return this;
}
public EventListener longClick(String method){
this.longClickMethod = method;
return this;
}
public EventListener itemLongClick(String method){
this.itemLongClickMehtod = method;
return this;
}
public EventListener itemClick(String method){
this.itemClickMethod = method;
return this;
}
public boolean onLongClick(View v) {
return invokeLongClickMethod(handler,longClickMethod,v);
}
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
return invokeItemLongClickMethod(handler,itemLongClickMehtod,arg0,arg1,arg2,arg3);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
invokeItemClickMethod(handler,itemClickMethod,arg0,arg1,arg2,arg3);
}
public void onClick(View v) {
invokeClickMethod(handler, clickMethod, v);
}
private static Object invokeClickMethod(Object handler, String methodName, Object... params){
if(handler == null) return null;
Method method = null;
try{
method = handler.getClass().getDeclaredMethod(methodName,View.class);
if(method!=null)
return method.invoke(handler, params);
else
throw new RuntimeException("no such method:"+methodName);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
private static boolean invokeLongClickMethod(Object handler, String methodName, Object... params){
if(handler == null) return false;
Method method = null;
try{
//public boolean onLongClick(View v)
method = handler.getClass().getDeclaredMethod(methodName,View.class);
if(method!=null){
Object obj = method.invoke(handler, params);
return obj==null?false:Boolean.valueOf(obj.toString());
}
else
throw new RuntimeException("no such method:"+methodName);
}catch(Exception e){
e.printStackTrace();
}
return false;
}
private static Object invokeItemClickMethod(Object handler, String methodName, Object... params){
if(handler == null) return null;
Method method = null;
try{
///onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
method = handler.getClass().getDeclaredMethod(methodName,AdapterView.class,View.class,int.class,long.class);
if(method!=null)
return method.invoke(handler, params);
else
throw new RuntimeException("no such method:"+methodName);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
private static boolean invokeItemLongClickMethod(Object handler, String methodName, Object... params){
if(handler == null) throw new RuntimeException("invokeItemLongClickMethod: handler is null :");
Method method = null;
try{
///onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
method = handler.getClass().getDeclaredMethod(methodName,AdapterView.class,View.class,int.class,long.class);
if(method!=null){
Object obj = method.invoke(handler, params);
return Boolean.valueOf(obj==null?false:Boolean.valueOf(obj.toString()));
}
else
throw new RuntimeException("no such method:"+methodName);
}catch(Exception e){
e.printStackTrace();
}
return false;
}
}
public abstract class BaseActvity extends FragmentActivity {
@Override
public void setContentView(int layoutResID) {
// TODO Auto-generated method stub
super.setContentView(layoutResID);
initInjectedView(this);
}
@Override
public void setContentView(View view) {
// TODO Auto-generated method stub
super.setContentView(view);
initInjectedView(this);
}
@Override
public void setContentView(View view, LayoutParams params) {
// TODO Auto-generated method stub
super.setContentView(view, params);
initInjectedView(this);
}
private void initInjectedView(Activity activity) {
initInjectedView(activity, activity.getWindow().getDecorView());
}
private void initInjectedView(Object activity, View sourceView) {
Field[] fields = activity.getClass().getDeclaredFields(); // 获取字段
if (fields != null && fields.length > 0) {
for (Field field : fields) {
try {
field.setAccessible(true); // 设为可访问
if (field.get(activity) != null)
continue;
InjectView mView = field.getAnnotation(InjectView.class);
if (mView != null) {
int viewId = mView.id();
if (viewId == 0)
viewId = getResources().getIdentifier(field.getName(), "id", getPackageName());
if (viewId == 0)
Log.e("D3Activity", "field " + field.getName() + "not found");
// 关键,注解初始化,相当于 backBtn = (TextView)
// findViewById(R.id.back_btn);
field.set(activity, sourceView.findViewById(viewId));
// 事件
setListener(activity, field, mView.click(), Method.Click);
setListener(activity, field, mView.longClick(), Method.LongClick);
setListener(activity, field, mView.itemClick(), Method.ItemClick);
setListener(activity, field, mView.itemLongClick(), Method.itemLongClick);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void setListener(Object activity, Field field, String methodName, Method method) throws Exception {
if (methodName == null || methodName.trim().length() == 0)
return;
Object obj = field.get(activity);
switch (method) {
case Click:
if (obj instanceof View) {
((View) obj).setOnClickListener(new EventListener(activity).click(methodName));
}
break;
case ItemClick:
if (obj instanceof AbsListView) {
((AbsListView) obj).setOnItemClickListener(new EventListener(activity).itemClick(methodName));
}
break;
case LongClick:
if (obj instanceof View) {
((View) obj).setOnLongClickListener(new EventListener(activity).longClick(methodName));
}
break;
case itemLongClick:
if (obj instanceof AbsListView) {
((AbsListView) obj).setOnItemLongClickListener(new EventListener(activity).itemLongClick(methodName));
}
break;
default:
break;
}
}
public enum Method {
Click, LongClick, ItemClick, itemLongClick
}
}
使用特方便:简化代码,提高效率,搬砖必备。。。
@InjectView(itemClick = "OnItemClick")ResizeGridView gridview1, gridview2, gridview3;
public void OnItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
switch (parent.getId()) {
case R.id.gridview1:
ToastUtil.showCustomToast(this, "gridview1:"+((EarnPointBean)parent.getAdapter().getItem(position)).title);
break;
case R.id.gridview2:
ToastUtil.showCustomToast(this, "gridview2:"+((EarnPointBean)parent.getAdapter().getItem(position)).title);
break;
case R.id.gridview3:
ToastUtil.showCustomToast(this, "gridview3:"+((EarnPointBean)parent.getAdapter().getItem(position)).title);
break;
}
}