gpt4 book ai didi

android - 当同一个ListView中有多个样式时,ListView项样式不断随机变化

转载 作者:行者123 更新时间:2023-11-29 18:06:29 25 4
gpt4 key购买 nike

我制作了一个聊天应用程序,其中聊天 View 由 ListView 中的多行组成,其中用户发送的消息显示为白色, friend 收到的消息显示为蓝色。

此行为工作正常,但是当我在变得非常大(例如 30 行)后滚动列表时,颜色变得困惑。我为每一行使用不同的布局,白行布局和蓝行布局。

当我多次上下滚动列表时,颜色不断随机切换(即我的一些消息是蓝色的,另一些是白色的,而另一端(和我聊天的 friend )变成白色,而其他消息以随机方式变成蓝色。

我会给你一个关于所用代码的预览:

这是我的自定义适配器中的 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 之外是相同的,其中一个使用白色图像,另一个使用蓝色图像。

最佳答案

我最近遇到了类似的问题。我所做的是删除

if (convertView == null) 

并返回 View 本身。上面的代码所做的是:它检查当前列表中是否存在以前的缓存。如果存在,它会重新使用以前存在的列表。希望对你有帮助

关于android - 当同一个ListView中有多个样式时,ListView项样式不断随机变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13171820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com