Android Listview项目在滚动时更改
正如标题所说,当我在ListView中滚动时,我的项目会不断切换行。 我已经在这里读到了关于同一问题的文章,但我不知道我做错了什么Android Listview项目在滚动时更改,android,listview,scroll,Android,Listview,Scroll,正如标题所说,当我在ListView中滚动时,我的项目会不断切换行。 我已经在这里读到了关于同一问题的文章,但我不知道我做错了什么 @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View row = convertView; RecordHolder holder = null;
@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;
}
请提前回复我们。关于。在创建视图时,您正在基于当前的
MessageObject
设置holder.ID
,而不是在循环视图时为每个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{…}
块之后。(或者将其合并到以下中,如果(o instanceof MessageObject){…
块用于清除代码。尝试将这些方法添加到适配器类中:
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
为什么o不是MessageObject?在这种情况下,为什么不显示任何内容(实际上意味着不更改可能存在的convertView)为什么OP“try this code”一个好的答案总是会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,也是为了SO的未来访客。我同意。谢谢你的反馈。