打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(一)基础篇

使用反射+注解实现ORM关系映射,类似hmb,Spring等。

老规矩,先看一下使用效果:

调用:

ListBinder.With(mRecyclerView).setLtnImpl(this).bind(news)
Pojo类

@ListDataSrc(R.layout.list_item_card_main)
public class NewsListPojo implements Serializable{
 
    @BindText(R.id.title)
    private String title;
    @BindText(R.id.content)
    private String content;
    @BindText(R.id.date)
    private String date;
    @BindAsyncImgUrl(R.id.cover)
    private String coverurl;
    @BindText(R.id.subcounts)
    private String subscriptCounts;
    @OnBtClick(R.id.subbt)
    private String subscriptButton;
 
 
 
    public NewsListPojo() {
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
    public String getContent() {
        return content;
    }
 
    public void setContent(String content) {
        this.content = content;
    }
 
    public String getDate() {
        return date;
    }
 
    public void setDate(String date) {
        this.date = date;
    }
 
    public String getCoverurl() {
        return coverurl;
    }
 
    public void setCoverurl(String coverurl) {
        this.coverurl = coverurl;
    }
 
    public String getSubscriptButton() {
        return subscriptButton;
    }
 
    public void setSubscriptButton(String subscriptButton) {
        this.subscriptButton = subscriptButton;
    }
 
    public String getSubscriptCounts() {
        return subscriptCounts;
    }
 
    public void setSubscriptCounts(String subscriptCounts) {
        this.subscriptCounts = subscriptCounts;
    }
}
怎么样 很方便吧?

先讲一下大体的实现思路:

0x0:首先从宏观思考一下怎样把ViewId与数据Value联系起来。

      0x00:反射获取类上(Type)的注解以获取Item的layoutId;

      0x01:反射便利pojo类中的成员(field)并获取其上的注解,从注解中获取对应的itemViewID;

      0x02:把上边这些信息存入某个Entity中;

      0x03:想办法抽象出一个通用的Adapter和ViewHolder,抽象出类似 public void adapterCall(ViewHolder holder, int position); 的callback

      0x04:在抽象出的adapterCall中invoke 0x02中Entity的信息

下面就来聊一下具体的。

1.首先应该想到每一对ORM映射对象应该都有一个实体类保存信息。而这些信息需要用到反射解析,所以考虑到性能,并且List每个Item一般类型都是相同的,所以应该在Adapter外解析完毕后再交由Adapter调用。

首先是实体类

public class BinderTarget{
        private Class type;
        private Method method;
        private Field field;

        private Object ltnImpl;
        private Method ltnMethod;
        private Class ltnType;

        public BinderTarget(Class type, Field field, Method method) {
            this.type = type;
            this.field = field;
            this.method = method;
        }

        public BinderTarget(Object ltnImpl, Method ltnMethod, Class ltnType) {
            this.ltnImpl = ltnImpl;
            this.ltnMethod = ltnMethod;
            this.ltnType = ltnType;
        }

        public Class getType() {
            return type;
        }

        public void setType(Class type) {
            this.type = type;
        }

        public Field getField() {
            return field;
        }

        public void setField(Field field) {
            this.field = field;
        }

        public Method getMethod() {
            return method;
        }

        public void setMethod(Method method) {
            this.method = method;
        }

        public Object getLtnImpl() {
            return ltnImpl;
        }

        public void setLtnImpl(Object ltnImpl) {
            this.ltnImpl = ltnImpl;
        }

        public Method getLtnMethod() {
            return ltnMethod;
        }

        public void setLtnMethod(Method ltnMethod) {
            this.ltnMethod = ltnMethod;
        }

        public Class getLtnType() {
            return ltnType;
        }

        public void setLtnType(Class ltnType) {
            this.ltnType = ltnType;
        }
    }
可以看到存储的变量就分为两个部分,一是普通的控件负值类似textview.setText(str);,二是为控件添加监听,也是view.setListener(lsn);

即要存储的是1.Methed(对应setText与setListener)

2.Field(每个item对应list元素中的成员变量,就是str对应的Filed)

3.Class(参数的类型,Invoke需要用到)


private SparseArray<BinderTarget> bindlist;


上面是ViewId与Bindertarget建立的映射关系。

第一篇的结尾 ,相信大家已经对实现方式有一些头绪了,那么就第二篇再做分解了微笑 源码集成在我的小框架里GitHub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值