正如标题所说,当我滚动浏览 ListView 时,我的项目会不断切换行。我已经在这里读到了同样的问题,但我不知道我做错了什么。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
RecordHolder holder = null;
Object o = getItem(position);
if (row == null) {
LayoutInflater inflater = activity.getLayoutInflater();
row = inflater.inflate(R.layout.chatmessageitem,null);
holder = new RecordHolder();
holder.txtMessage = (TextView) row.findViewById(R.id.txttextmessage);
holder.messageBubble = (ImageView) row.findViewById(R.id.chatbubble);
if(o instanceof MessageObject){
MessageObject m = (MessageObject) o;
if(m.getEmpfänger() != ((GlobalClass) context).myChatID){
holder.ID = m.getEmpfänger();
}else{
holder.ID =((GlobalClass) context).myChatID;
}
holder.message =(m.getMessage());
}
row.setTag(holder);
}else{
holder = (RecordHolder)row.getTag();
}
if(o instanceof MessageObject){
MessageObject msg = (MessageObject)o;
holder.txtMessage.setText(msg.getMessage());
if(holder.ID != ((GlobalClass) context).myChatID){
holder.messageBubble.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bubbleone));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
holder.txtMessage.setText(holder.message);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.messageBubble.setLayoutParams(lp);
}else{
holder.messageBubble.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bubbletwo));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
holder.txtMessage.setText(holder.message);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.messageBubble.setLayoutParams(lp);
}
}
return row;
}
static class RecordHolder {
String message;
int ID;
TextView txtMessage;
ImageView messageBubble;
}
thx 提前提供任何答案。问候。