2015-11-12 68 views
0

GetView方法正確設置數據。我檢查了我的mMessageList項目標籤並且膨脹了相應的佈局,但在滾動getView混合它們。我該如何解決這個問題?爲什麼getView混合我的行?

public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder viewHolder = null; 

    if(convertView == null){ 
     viewHolder = new ViewHolder(); 
     if(mMessageList.get(position).getTag() == RespKey.MESSAGE_TAG_CUSTOMER){ 
      convertView = mInflater.inflate(R.layout.message_list_row_customer, null); 
      viewHolder.messageTextView = (TextView) convertView.findViewById(R.id.userMessageText); 
      viewHolder.messageImageView = (ImageView) convertView.findViewById(R.id.userMessageIcon); 
     } else { 
      convertView = mInflater.inflate(R.layout.message_list_row_driver, null); 
      viewHolder.messageTextView = (TextView) convertView.findViewById(R.id.driverMessageText); 
      viewHolder.messageImageView = (ImageView) convertView.findViewById(R.id.driverMessageIcon); 
     } 
     convertView.setTag(viewHolder); 
     } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 


    viewHolder.messageTextView.setText(mMessageList.get(position).getMessage()); 
    return convertView; 
} 

public static class ViewHolder{ 

    public TextView messageTextView; 
    public ImageView messageImageView; 
} 

回答

1

你必須覆蓋getViewTypeCountgetItemViewType這種使用情況。前者返回您需要的不同convertView的數量。在你的情況下它應該是2.後者返回convertView的類型。 E.x.

@Override 
public int getItemViewType(int position) { 
     if (mMessageList.get(position).getTag() == RespKey.MESSAGE_TAG_CUSTOMER) { 
      return 0; 
     } 
     return 1; 
} 

getItemViewType始終以0getViewTypeCount - 1之間返回一個int。這個非常重要。在內部,該值用於訪問數組convertView

+0

我將返回0和1或RespKey.MESSAGE_TAG_CUSTOMER和RespKey.MESSAGE_TAG_DRIVER? – user1480210

+0

如果RespKey.MESSAGE_TAG_CUSTOMER和RespKey.MESSAGE_TAG_DRIVER是int值爲0和1,則應返回常量。 – Blackbelt

+0

謝謝,這是解決我的問題 – user1480210

相關問題