Android中ListView多种Item布局

类似 经常 使用的 一套 布局一个ViewHolder(持有 conterView的子View),多增加了 若干ViewHolder

结构 例如

        Message msg = myList.get(position);  
        int type = getItemViewType(position);  

        ViewHolderTime holderTime = null;  
        ViewHolderRightText holderRightText = null;  
        ViewHolderRightImg holderRightImg = null;  
      
          
        if (convertView == null) {  
            switch (type) {  
            case VALUE_TIME_TIP:  
                holderTime = new ViewHolderTime();  
                convertView = mInflater.inflate(R.layout.list_item_time_tip,  
                        null);  
                holderTime.tvTimeTip = (TextView) convertView  
                        .findViewById(R.id.tv_time_tip);  
                holderTime.tvTimeTip.setText(msg.getValue());  
                convertView.setTag(holderTime);  
                break;  
            // 左边  
            case VALUE_LEFT_TEXT:  
                holderLeftText = new ViewHolderLeftText();  
                convertView = mInflater.inflate(R.layout.list_item_left_text,  
                        null);  
                holderLeftText.ivLeftIcon = (ImageView) convertView  
                        .findViewById(R.id.iv_icon);  
                holderLeftText.btnLeftText = (Button) convertView  
                        .findViewById(R.id.btn_left_text);  
                holderLeftText.btnLeftText.setText(msg.getValue());  
                convertView.setTag(holderLeftText);  
                break;  
  
            case VALUE_LEFT_IMAGE:  
                holderLeftImg = new ViewHolderLeftImg();  
                convertView = mInflater.inflate(R.layout.list_item_left_iamge,  
                        null);  
                holderLeftImg.ivLeftIcon = (ImageView) convertView  
                        .findViewById(R.id.iv_icon);  
                holderLeftImg.ivLeftImage = (ImageView) convertView  
                        .findViewById(R.id.iv_left_image);  
                holderLeftImg.ivLeftImage.setImageResource(R.drawable.test);  
                convertView.setTag(holderLeftImg);  
                break;  
            }  
              
        } else {  
            switch (type) {  
            case VALUE_TIME_TIP:  
                holderTime=(ViewHolderTime)convertView.getTag();  
                holderTime.tvTimeTip.setText(msg.getValue());  
                break;  
            case VALUE_LEFT_TEXT:  
                holderLeftText=(ViewHolderLeftText)convertView.getTag();  
                holderLeftText.btnLeftText.setText(msg.getValue());  
                break;  
            case VALUE_LEFT_IMAGE:  
                holderLeftImg=(ViewHolderLeftImg)convertView.getTag();  
                holderLeftImg.ivLeftImage.setImageResource(R.drawable.test);  
                break;  
            }  
       
        }  
        return convertView;  


 
  

你可能感兴趣的:(android,android,ListView)