打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(三)具体实现
来源:互联网 发布:92game仿影视大全源码 编辑:程序博客网 时间:2024/06/05 12:00
在上面一篇中我们已经获得了所有必要的数据,下面就是在抽象好的onBinder回掉中调用这些method。
1.首先补一下上一篇的坑,Pojo类的解析过程如下
public void bind(AbsListView view,RecyclerView recyclerView,List<?> list,Class clazzType,Object ltnImpl){ if (list == null) return; Class clazz = null; if (list.size() != 0) clazz = list.get(0).getClass(); else clazz = clazzType; ListDataSrc listDataSrc = (ListDataSrc) clazz.getAnnotation(ListDataSrc.class); if (listDataSrc == null) return; List<ClassMemberPackage> mems = null; try { mems = ClassMemberParase.GetFileds(list.get(0)); } catch (Exception e) { e.printStackTrace(); } if (mems == null||mems.size()==0) return; final BinderPackage binderPackage = new BinderPackage(); binderPackage.setList(list); for (ClassMemberPackage mem : mems){ Annotation[] annotations = mem.getAnnotations(); for (Annotation anno : annotations){ ListBinderBase annobase = anno.annotationType() .getAnnotation(ListBinderBase.class); ListBinderLtnBase ltnannobase = anno.annotationType() .getAnnotation(ListBinderLtnBase.class); if (annobase!=null){ Method method = null; try { method = annobase.viewType().getDeclaredMethod(annobase.methodName(),annobase.dataType()); } catch (NoSuchMethodException e) { e.printStackTrace(); } if (method != null) { BinderPackage.BinderTarget target = binderPackage.new BinderTarget(annobase.viewType(), mem.getField(), method); for (int item : getItems(anno)) binderPackage.getBindlist().put(item, target); } } if (ltnannobase!=null){ Method method = null; try { method = ltnannobase.viewType().getDeclaredMethod(ltnannobase.listenerSetter(),ltnannobase.listenerType()); } catch (NoSuchMethodException e) { e.printStackTrace(); } if (method==null) continue; Object ltn = ltnImpl; BinderPackage.BinderTarget target = binderPackage.new BinderTarget(ltn,method,ltnannobase.listenerType()); for (int item:getItems(anno)){ if (binderPackage.getBindlist().get(item)==null){ binderPackage.getBindlist().put(item,target); }else { BinderPackage.BinderTarget tar = binderPackage.getBindlist().get(item); tar.setLtnImpl(ltn); tar.setLtnMethod(method); tar.setLtnType(ltnannobase.listenerType()); } } } } }
ClassMemberParse是我实现的用于解析对象成员变量的工具类,集成在我的框架中,后期可以将所有反射方法集成在里面,以便于添加缓存,提高框架解析速度。基本过程就是解析注解里的几个参数,加上一些判断,比较重要的是如果客户不想使用简单的ORM映射,可以自己实现onBinder回掉,这样就没有必要解析pojo类了。
2.onBinder的具体细节,这里参考了鸿翔老师的通用ViewHolder抽象出通用的onBinder回掉接口,并在此方法里使用前面解析的数据。
public void setHolder(IViewHolder holder, int position){ for (int i = 0;i < bindlist.size();i++){ int key = bindlist.keyAt(i); BinderTarget bt = bindlist.get(key); View view = holder.getView(key); Field field = bt.getField(); Object value = null; if (field != null) { try { value = field.get(list.get(position)); } catch (IllegalAccessException e) { e.printStackTrace(); } if (value == null) return; } Method method = bt.getMethod(); Method ltnmethod = bt.getLtnMethod(); Object ltnImpl = bt.getLtnImpl(); try { if (method != null) method.invoke(view,value); if (ltnmethod != null) { ltnmethod.invoke(view, ltnImpl); view.setTag(position); } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } public void setHolder(ViewHolder holder, int position){ for (int i = 0;i < bindlist.size();i++){ int key = bindlist.keyAt(i); BinderTarget bt = bindlist.get(key); View view = holder.getView(key); Field field = bt.getField(); Object value = null; if (field != null) { try { value = field.get(list.get(position)); } catch (IllegalAccessException e) { e.printStackTrace(); } if (value == null) return; } Method method = bt.getMethod(); Method ltnmethod = bt.getLtnMethod(); Object ltnImpl = bt.getLtnImpl(); try { if (method != null) method.invoke(view,value); if (ltnmethod != null) { ltnmethod.invoke(view, ltnImpl); view.setTag(position); } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
两个setBinder分别对应AbsListView和RecycleView。总体来说代码还不怎么健壮,但是基本过程都表述的差不多了
最后是对外调用的接口,链式调用比较美观。
public class ExtCall{ private AbsListView listView; private RecyclerView recyclerView; private Class clazz; private IAdapterCallBack callBack; private IRcAdapterCallBack rcCallBack; private Object ltnImpl; public ExtCall(AbsListView listView) { this.listView = listView; } public ExtCall(RecyclerView recyclerView) { this.recyclerView = recyclerView; } public ExtCall setClass(Class clazz){ this.clazz = clazz; return this; } public ExtCall setCallBack(IAdapterCallBack callBack){ this.callBack = callBack; return this; } public ExtCall setCallBack(IRcAdapterCallBack callBack){ this.rcCallBack = callBack; return this; } public ExtCall setLtnImpl(Object ltnImpl) { this.ltnImpl = ltnImpl; return this; } public void bind(List<?> list){ if (listView!=null){ if (callBack == null) getInstance().bind(listView,recyclerView,list,clazz,ltnImpl); else getInstance().bind(listView,list,callBack,clazz); } if (recyclerView!=null){ if (rcCallBack == null) getInstance().bind(listView,recyclerView,list,clazz,ltnImpl); else getInstance().bind(recyclerView,list,rcCallBack,clazz); } }
1 0
- 打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(三)具体实现
- 打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(一)基础篇
- 打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(二)ORM注解以及解析
- 为RecycleView添加ContextMenu支持
- TwinklingRefreshLayout 支持下拉刷新和上拉加载的 RefreshLayout,自带越界回弹效果,支持 RecyclerView,AbsListView,ScrollView等
- android异步的网络图片加载,支持多线程异步加载
- 一个支持下拉自动加载的recycleview
- android 添加 iconv 支持
- android 添加curl支持
- jquery.simple.tree 支持无限级分类,异步加载数据
- android 拼音搜索联系人分析与实现(支持多音字,支持T9搜索,支持Qwerty搜索)
- Extjs RowExpander的扩展,支持异步加载
- TwinklingRefreshLayout 支持下拉刷新和上拉加载的 RefreshLayout,自带越界回弹效果,支持 RecyclerView,AbsListView,ScrollView,We
- 打造支持加密与odbc的sqlite
- Android添加鼠标支持研究
- android添加 Ethernet框架支持
- android添加 Ethernet框架支持
- android 添加 c++ 11 支持
- [leetcode] 【链表】 138. Copy List with Random Pointer
- JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
- iOS Plus屏幕 AlerController报错
- RT-Thread的I/O设备模块及其驱动实现步骤
- Linux系统用户账号的管理
- 打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(三)具体实现
- Eclipse快捷键-方便查找
- 求0至大数之间的随机数
- 禁用VS2010的IntelliSense
- 架构——秒杀系统架构分析与实战
- c++内存泄漏检测
- 关于R与tableau的集成----kmeans聚类
- AngularJS Post 表单键值对格式
- Android studio使用过程中遇到的问题解决汇总