Android基础--ListView的复用机制

RecycleBin 机制

RecycleBin 缓存机制如图:
Android基础--ListView的复用机制_第1张图片
RecycleBin 代码实现:其实就是BaseAdapter中的getView方法的第二个参数convertView复用代码如下:这是两种样式的item的复用

@Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        if (getItemViewType(position) == ViewType.TEXT){
            if (convertView == null){
                convertView = inflater.inflate(R.layout.text_message_item,viewGroup,false);
            }
            TextView textView = convertView.findViewById(R.id.message_content);
            TextMessage textMessage = (TextMessage)messageList.get(position);
            textView.setText(textMessage.getText());
        }else {
            if (convertView == null){
                convertView = inflater.inflate(R.layout.image_message_item,viewGroup,false);
            }
            ImageView imageView = convertView.findViewById(R.id.image_head);
            ImageMessage imageMessage = (ImageMessage)messageList.get(position);
            imageView.setImageResource(imageMessage.getImage());
        }
        return convertView;
    }

ViewHolder机制

如上面代码中每次都会去findViewById这项操作,findViewById其实是很耗时的一个操作,ViewHolder机制就是把findViewById的结果缓存起来,例子中有两ViewHolder : ViewHolderText 与 ViewHolderImage对应两种item


public class MyBaseAdapter extends BaseAdapter {

    private static final int ViewTypeCount = 2;
    private interface ViewType {
        int TEXT = 0;
        int IMAGE = 1;
    }


    private List messageList;

    public MyBaseAdapter(List messageList) {
        this.messageList = messageList;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        int type = getItemViewType(position);
        ViewHolderText viewHolderText = null;
        ViewHolderImage viewHolderImage = null;
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        if (type == ViewType.TEXT){
            if (convertView == null){
                convertView = inflater.inflate(R.layout.text_message_item,viewGroup,false);
                viewHolderText = new ViewHolderText();
                viewHolderText.textMessage = convertView.findViewById(R.id.message_content);
                convertView.setTag(viewHolderText);
            }
            TextMessage textMessage = (TextMessage)messageList.get(position);
            viewHolderText = (ViewHolderText)convertView.getTag();
            viewHolderText.textMessage.setText(textMessage.getText());
        }else {
            if (convertView == null){
                convertView = inflater.inflate(R.layout.image_message_item,viewGroup,false);
                viewHolderImage = new ViewHolderImage();
                viewHolderImage.imageView = convertView.findViewById(R.id.image_head);
                convertView.setTag(viewHolderImage);
            }
            ImageMessage imageMessage = (ImageMessage)messageList.get(position);
            viewHolderImage = (ViewHolderImage)convertView.getTag();
            viewHolderImage.imageView.setImageResource(imageMessage.getImage());
        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position) instanceof TextMessage){
            return ViewType.TEXT;
        }else {
            return ViewType.IMAGE;
        }
    }

    @Override
    public int getViewTypeCount() {
        return ViewTypeCount;
    }

    class ViewHolderText{
        private TextView textMessage;
    }

    class ViewHolderImage{
        private ImageView imageView;
    }
}
 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(Android高级UI)