正如标题所示,当我滚动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;
}
提前回答任何问题。问候。
答案 0 :(得分:1)
您在创建视图时根据当前holder.ID
设置MessageObject
,而不是在视图被回收时为每个MessageObject
设置MessageObject
。然后根据该初始(可能是错误的)ID设置内容,但使用当前 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());
}
。
移动此逻辑:
if (row == null) {...} else {...}
到if(o instanceof MessageObject){...
块之后。 (或者将其合并到以下{{1}}块中以获得更清晰的代码。
答案 1 :(得分:0)
尝试将这些方法添加到适配器类:
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}