2012-03-14 12 views
0

私は2つのビューを持つリストビューを持っています.1つは最初の行で、もう1つは残りのものです。ここでandroid listview multiple views

はgetViewメソッドで私のコードです:

ViewHolder holder = null; 

      // if (convertView == null) 
     // { 
       holder = new ViewHolder(); 

       if(position==0 && category.equalsIgnoreCase("normal")) 
       { 
        convertView = mInflater.inflate(R.layout.item_featured_list_row, null);           
       } 
       else 
       {     
        convertView = mInflater.inflate(R.layout.item_list_row, null);   
       }  

        holder.imgItem = (ImageView) convertView.findViewById(R.id.itemImage); 
        //holder.imgArrow = (ImageView) convertView.findViewById(R.id.arrowImage); 
        holder.txtItem = (TextView) convertView.findViewById(R.id.itemText); 
        holder.itemValueLabel = (TextView) convertView.findViewById(R.id.itemValueLabel); 
        holder.itemPrice = (TextView) convertView.findViewById(R.id.itemPrice);        

       convertView.setTag(holder); 
     // } 
     // else 
     //  holder = (ViewHolder) convertView.getTag(); 

事はある、私はコメントを外し、それを残せば、それだけでこのように動作しますが、convertViewがnullであるかどうかを確認するための良い理由があるように私は感じ、既に膨らませたものを再膨張させない。しかし、コメントを外すと、最初の読み込み時にのみ機能します。配列を更新してnotifydatasetchangedを呼び出してリストを更新すると、最初の行項目はリストビューのランダムな場所で複数回終了します。

答えて

0

異なるレイアウトを使用する場合は、実際にconvertViewを再利用できるかどうかを確認する必要があります。ホルダーにタイプインジケーターを追加して、convertViewのタイプが変更可能かどうかを確認することができます。新しいビューを展開しない場合。

// check type - use int if you need more than just two layouts. 
boolean requiredViewType = (position == 0 && category.equalsIgnoreCase("normal")); 

if (convertView == null || convertView.getTag().viewType != requiredViewType) { 
    holder = new ViewHolder(); 

    if (position == 0 && category.equalsIgnoreCase("normal")) { 
     convertView = mInflater.inflate(R.layout.item_featured_list_row, null); 
    } else { 
     convertView = mInflater.inflate(R.layout.item_list_row, null); 
    } 

    holder.imgItem = (ImageView) convertView.findViewById(R.id.itemImage); 
    // holder.imgArrow = (ImageView) 
    // convertView.findViewById(R.id.arrowImage); 
    holder.txtItem = (TextView) convertView.findViewById(R.id.itemText); 
    holder.itemValueLabel = (TextView) convertView.findViewById(R.id.itemValueLabel); 
    holder.itemPrice = (TextView) convertView.findViewById(R.id.itemPrice); 

    // set the type - you obviously need to add that field to ViewHolder 
    holder.viewType = requiredViewType; 

    convertView.setTag(holder); 
} else { 
    holder = (ViewHolder) convertView.getTag(); 
    // etc 
}