ViewInject原理
来源:互联网 发布:e63诺基亚软件下载网址 编辑:程序博客网 时间:2024/06/16 21:09
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.app.Activity;
import android.view.View;
/***
* 注解管理器
* @author Administrator
*
*/
public class ViewInjectUtils
{
private static final String METHOD_SET_CONTENTVIEW = "setContentView";
private static final String METHOD_FIND_VIEW_BY_ID = "findViewById";
/**
*Activity中使用
* **/
public static void inject(Activity activity)
{
injectContentView(activity);
injectViews(activity,null);
injectOnclickView(activity,null);
}
/**
* 注释
* @param parent 带有注解的类
* @param view 所在的View
*/
public static void inject(Object parent,View view)
{
injectViews(parent,view);
injectOnclickView(parent,view);
}
/**
* 注入所有的控件
*
* @param view
*/
private static void injectViews(Object view,Object targetView)
{
Class clazz = view.getClass();
Field[] fields = clazz.getDeclaredFields();
// 遍历所有成员变量
for (Field field : fields)
{
ViewInject viewInjectAnnotation = field
.getAnnotation(ViewInject.class);
if (viewInjectAnnotation != null)
{
int viewId = viewInjectAnnotation.value();
if (viewId != -1)
{
// 初始化View
try
{
Method method = null;
Object resView = null;
if(targetView != null)
{
method = targetView.getClass().getMethod(METHOD_FIND_VIEW_BY_ID,
int.class);
resView = method.invoke(targetView, viewId);
}
else
{
method = clazz.getMethod(METHOD_FIND_VIEW_BY_ID,
int.class);
resView = method.invoke(view, viewId);
}
if(resView == null)
{
continue;
}
field.setAccessible(true);
field.set(view, resView);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
/**
* 注入主布局文件
*
* @param activity
*/
private static void injectContentView(Activity activity)
{
Class<? extends Activity> clazz = activity.getClass();
// 查询类上是否存在ContentView注解
ContentView contentView = clazz.getAnnotation(ContentView.class);
if (contentView != null)// 存在
{
int contentViewLayoutId = contentView.value();
try
{
Method method = clazz.getMethod(METHOD_SET_CONTENTVIEW,
int.class);
method.setAccessible(true);
method.invoke(activity, contentViewLayoutId);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
private static void injectOnclickView(Object view,Object targetView)
{
Class clazz = view.getClass();
Method[] ms = clazz.getMethods();
for(Method m:ms)
{
ViewOnClick viewClick = m.getAnnotation(ViewOnClick.class);
if(viewClick != null)
{
try
{
int[] ids = viewClick.value();
for (int id : ids) {
Method method = null;
Object obj = null;
if(targetView != null)
{
method = targetView.getClass().getMethod(METHOD_FIND_VIEW_BY_ID,
int.class);
obj = method.invoke(targetView, id);
}
else
{
method = clazz.getMethod(METHOD_FIND_VIEW_BY_ID,
int.class);
obj = method.invoke(view, id);
}
if (null == obj) {
continue;
}
if(!View.class.isAssignableFrom(obj.getClass())){
continue;
}
((View) obj).setOnClickListener(new EventListener(view).click(m.getName()));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 查找View 相当于findviewbyid
* @author Administrator
*
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ViewInject
{
int value();
}
- ViewInject原理
- 巧用ViewInject
- Android 通过 annotation ViewInject
- Android 通过 annotation ViewInject
- @SuppressWarinings, @TargetApi, @SuppressLint, @ViewInject
- android viewinject 反射 注解 IOC
- Android 中的 IOC 框架 【ViewInject】
- Android 中的 IOC 框架 【ViewInject】
- Android注解IOC框架【ViewInject】(一)
- xUtils系列之ViewUtils-ViewInject注解
- 不需要ViewInject,简化你的findViewById
- Android 进阶 Android 中的 IOC 框架 【ViewInject】
- 记录Android 中的 IOC 框架 【ViewInject】
- IOC开发框架[@ViewInject] 注解式发开
- Android IOC 框架ViewInject(上)
- 注解+反射实现ViewInject的功能
- 安卓进阶之自己实现 ViewInject框架
- Android中怎样用@ViewInject注解来彻底放弃findViewByid,gogoog0!!!
- 基于spark实现的CRF模型的使用与源码分析
- hdu2044 一只小蜜蜂....
- A. Nicholas and Permutation
- NDIS Filter Study
- 地图寻宝
- ViewInject原理
- 死锁的产生以及如何防止死锁的产生
- trickyPriorityQueue
- python 装饰器传参
- [leetcode] 【链表】 146. LRU Cache
- leetcode Clone Graph
- hdu2602-01背包
- java 反射机制
- TimesTen 数据库复制学习:4. 定义Active Standby Pair复制策略