0

正如标题所说,当我滚动浏览 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 提前提供任何答案。问候。

4

2 回答 2

1

您在创建视图时holder.ID基于当前设置MessageObject,而不是MessageObject在视图被回收时为每个设置。然后根据该初始(可能是错误的)ID 设置内容,但使用当前的MessageObject.

移动这个逻辑:

    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){...块中以获得更清晰的代码。

于 2013-10-29T19:53:11.383 回答
0

尝试将这些方法添加到您的适配器类:

 @Override
  public int getViewTypeCount() {
   return getCount();
  }

  @Override
  public int getItemViewType(int position) {
   return position;
  }
于 2016-09-01T13:06:00.313 回答