码迷,mamicode.com
首页 > 移动开发 > 详细

Android ListView优化 如何省略ViewHolder方法

时间:2015-01-05 18:45:31      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:listview   viewholder   baseadapter   getview   

代码:
public class ViewHolder {
    /**
     * @param view converView
     * @param id 控件的id
     * @return 返回<T extends View>
     */
    public static <T extends View> T get(View view, int id) {
        SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
        if (null == viewHolder) {
            viewHolder = new SparseArray<View>();
            view.setTag(viewHolder);
        }
        View childView = viewHolder.get(id);
        if (null == childView) {
            childView = view.findViewById(id);
            viewHolder.put(id, childView);

        }
        return (T) childView;
    }

}

在Adapter使用:

   @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String keyName = getItem(position);
        if (null == convertView) {
            convertView = inflate(R.layout.layout_comm_custom_search_item, null);
        }
        TextView searchKeyName = (TextView)ViewHolder.get(convertView, R.id.search_key_name);
        searchKeyName.setText(keyName);
        return convertView;
    }


Android ListView优化 如何省略ViewHolder方法

标签:listview   viewholder   baseadapter   getview   

原文地址:http://blog.csdn.net/chenlove1/article/details/42425529

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!