博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

android 一般都用viewholder来优化contentView,采用sparseArray能够进一步优化

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
     * 用法: ImageView bananaView = ViewHolder.get(convertView, R.id.banana);
     *
     * @param convertView
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends View> T getView(View convertView, int id) {
        SparseArray<View> viewHolder = (SparseArray<View>) convertView.getTag();
        if (viewHolder == null) {
            viewHolder = new SparseArray<View>();
            convertView.setTag(viewHolder);
        }
        View childView = viewHolder.get(id);
        if (childView == null) {
            childView = convertView.findViewById(id);
            viewHolder.put(id, childView);
        }
        return (T) childView;
    }

  

posted on   Likwo  阅读(953)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示