The role of ConverView in BaseAdapter and the optimization of ListView

    BaseAdapter中的ConverView的作用和ListView的优化

Each item in the ListView is a view. When an item slides out of the visible area, the view corresponding to this item will be recycled. This view is the converview. If the converview is not applicable, every time the item is removed from the screen , A new view is added to the item, which increases the cost, so the converview can reuse the newly existing items.
Regarding optimization, if the layout of each item is very complicated, finding the control through findViewById will undoubtedly increase the system overhead. By referencing the Tag and using getTag to obtain the bound ViewHolder object, the ViewHolder object stores the controls in the layout and is selected by ViewHolder Controls to avoid heavy screening and improve efficiency. Not
much to say, the code:
private class MyAdapter extends BaseAdapter{

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int position)
    {
        return position;
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {   ViewHolder viewHolder;
        LayoutInflater inflater=LayoutInflater.from(getActivity());
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView =inflater.inflate(R.layout.listitem,null);
            viewHolder.picture=(ImageView)convertView.findViewById(R.id.thumbnail);
            viewHolder.title=(TextView)convertView.findViewById(R.id.title);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder=(ViewHolder)convertView.getTag();
        }
        viewHolder.picture.setImageBitmap((Bitmap)list.get(position).get("thumbNail"));
        viewHolder.title.setText((String)list.get(position).get("title"));
        return convertView;
    }
}

private class ViewHolder{
ImageView picture;
TextView title;
}
}

Guess you like

Origin blog.csdn.net/skateboard1/article/details/44514465