2012-11-01 35 views
1

我已經創建了一個聊天視圖,其中由ListView中的許多行構成的聊天應用程序由用戶發送的消息以白色顯示,朋友顯示爲藍色。當ListView項目樣式在同一個ListView中有多個樣式時會隨機更改

此行爲工作正常,但當我變成真正大的(例如30行)滾動列表時,顏色被混淆了。我爲每一行使用不同的佈局,白色的行佈局和藍色的行佈局。

當我在列表中上下滾動多次時,顏色會隨機切換(即,我的一些消息是藍色的,其他消息是白色的,另一端(與我聊天的朋友)變成白色和其他消息成爲了一個隨機的方式藍色

我給你所使用的代碼剪斷預覽:

這是我的自定義適配器的getView方法延伸BaseAdapter

public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     final int pos = position;       
     String from = messages.get(pos).getFrom(); 
     if (convertView == null) { 
      holder = new ViewHolder();  
        // If message is sent by me (the user) 
      if(from.toString().toLowerCase().equals("me")){ 
       convertView = mInflater.inflate(R.layout.chat_msg_row2, null); 
      } 
        // if message is sent by his friend (the other end user) 
      else{ 
       convertView = mInflater.inflate(R.layout.chat_msg_row, null);   
      } 

      holder.userName = (TextView) convertView.findViewById(R.id.msgusername); 
      holder.userImage = (ImageView) convertView.findViewById(R.id.ppic); 
       holder.date = (TextView) convertView.findViewById(R.id.msgdate); 
      holder.text = (TextView) convertView.findViewById(R.id.msg); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     }   
     holder.userName.setText(messages.get(pos).getFrom()); 
     holder.text.setText(messages.get(pos).getText()); 
     holder.date.setText(messages.get(pos).getDate());   
     String img = messages.get(pos).getImage();   
     if(img.substring(0,4).equalsIgnoreCase("http")){ 

      try{ 
        ImageLoader imloader = new ImageLoader(ChatActivity.this);  
          holder.userImage.setTag(img); 
       imloader.DisplayImage(img, ChatActivity.this , holder.userImage); 
       } 
      catch(Exception e){ 
       e.printStackTrace();      
      } 
     } 

     return convertView; 
    } 

    public long getItemId(int position){ 
     return 1; 
    } 

    public class ViewHolder{ 
     TextView userName; 
     ImageView userImage; 
     TextView text; 
     TextView date; 
    } 

兩種佈局chat_msg_row2chat_msg_row除了使用Drawable之外都是相同的,其中一個使用白色圖像,另一個使用藍色圖像。

回答

1

由於您沒有使用適配器的getItemViewTypegetViewTypeCount方法,那麼ListView假設你只有一個類型的列(儘管你實際使用兩種類型),併爲它們在列表中出現將回收行。因此,當convertView不是null時,您會得到一個再循環行,根據其位置可能是這兩種類型中的一種。

但是,只有當你的行真的不同時才應該實現上面的方法(額外的小部件,放置規則等)。如果兩行不同只能由背景圖片,那麼你只需要一個行,你會被設置根據您的應用程序的邏輯的背景下,這樣的事情:

//... 
if (convertView == null) { 
    holder = new ViewHolder();  
    // just inflate one of the rows if they are identical 
    convertView = mInflater.inflate(R.layout.chat_msg_row, parent, false);   
    holder.userName = (TextView) convertView.findViewById(R.id.msgusername); 
    holder.userImage = (ImageView) convertView.findViewById(R.id.ppic); 
    holder.date = (TextView) convertView.findViewById(R.id.msgdate); 
    holder.text = (TextView) convertView.findViewById(R.id.msg); 
    convertView.setTag(holder); 
} else { 
    holder = (ViewHolder) convertView.getTag(); 
}   
// set the background image as desired 
if(from.toString().toLowerCase().equals("me")){ 
    // I assumed you set the background on the root of the View row, convertView 
    // otherwise search for the desired view in convertView and set the background 
    convertView.setBackgroundResource(R.drawable.the_me_background_drawable); 
} else { 
    convertView.setBackgroundResource(R.drawable.the_other_user_background_drawable); 
} 
// ... 
2

我最近遇到了類似的問題。我所做的是刪除

if (convertView == null) 

並返回視圖本身。上面的代碼所做的是:它檢查當前列表中是否存在先前的緩存。如果存在,則重新使用之前的列表。希望它有幫助

相關問題