Release 2.0.1Beta
This commit is contained in:
@@ -3,11 +3,14 @@ package com.stardust.view;
|
||||
import android.view.View;
|
||||
import android.widget.CompoundButton;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/1/30.
|
||||
*
|
||||
* 哈?你说我为什么不用AA框架?之前还不知道嘛所以现在用了。
|
||||
*/
|
||||
|
||||
public class ViewBinder {
|
||||
@@ -21,6 +24,7 @@ public class ViewBinder {
|
||||
throw new RuntimeException("You must implement findViewById to use view binding", e);
|
||||
}
|
||||
Method[] methods = o.getClass().getDeclaredMethods();
|
||||
bindId(o, findViewById);
|
||||
for (Method method : methods) {
|
||||
method.setAccessible(true);
|
||||
bindClick(o, method, findViewById);
|
||||
@@ -28,6 +32,20 @@ public class ViewBinder {
|
||||
}
|
||||
}
|
||||
|
||||
private static void bindId(Object o, Method findViewById) {
|
||||
for (Field field : o.getClass().getDeclaredFields()) {
|
||||
field.setAccessible(true);
|
||||
ViewBinding.Id id = field.getAnnotation(ViewBinding.Id.class);
|
||||
if (id == null || id.value() == 0)
|
||||
continue;
|
||||
try {
|
||||
field.set(o, findViewById.invoke(o, id.value()));
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void bindCheck(final Object o, final Method method, Method findViewById) {
|
||||
ViewBinding.Check annotation = method.getAnnotation(ViewBinding.Check.class);
|
||||
if (annotation == null || annotation.value() == 0)
|
||||
|
||||
@@ -32,4 +32,10 @@ public @interface ViewBinding {
|
||||
@interface Check {
|
||||
int value();
|
||||
}
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface Id {
|
||||
int value();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user