ListView 如何提高其效率

1、ListView 如何提高其效率?
**当 convertView 为空时,用 setTag()方法为每个 View 绑定一个存放控件的 ViewHolder 对象
convertView不为空, 重复利用已经创建的view的时候, 使用getTag()方法获取绑定的ViewH
对象,这样就避免了findViewById对控件的层层查询,而是快速定位到控件。**
① 复用ConvertView,使用历史的view,提升效率200%
② 自定义静态类ViewHolder,减少findViewById 的次数。提升效率50%
③ 异步加载数据,分页加载数据。
④ 使用WeakRefrence引用ImageView对象

关键代码
`public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
TextView textView = null;

    if(convertView == null){    //① 复用ConvertView,使用历史的view,提升效率200%
        convertView = layoutInflater.inflate(listviewItem, null);
        imageView = (ImageView) convertView.findViewById(R.id.imageView1);
        textView = (TextView) convertView.findViewById(R.id.textView1);
        //② 自定义静态类ViewHolder,减少findViewById 的次数。提升效率50%
        convertView.setTag(new DataWrapper(imageView, textView));
    }else{
        DataWrapper dataWrapper = (DataWrapper) convertView.getTag();
        imageView = dataWrapper.imageView;
        textView = dataWrapper.textView;    
    }
    Contact contact = data.get(position);
    textView.setText(contact.name);
    //异步加载图片数据
    asyncImageLoad(imageView, contact.image);
    return convertView;
}`
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值