问题描述
我聊天应用
在自定义的阵列适配器。我设置随机颜色,以用户名与此code分别消息:
String []数组= context.getResources()getStringArray(R.array.username_colors)。
randomColor =阵列[新的随机()nextInt(array.length)];
nameTextView.setTextColor(Color.parseColor(randomColor));
当新邮件到达我把它添加到列表视图,并调用adapter.notifyDataSetChanged();
这导致每次重新着色现有的用户名的消息。
我怎样才能prevent重新着色的列表视图中调用adapter.notifyDataSetChanged()时,现有的项目;
修改1
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
ChatMessageElement EL = list.get(位置);
ViewHolder支架=无效;
NewMessagesLabelHolder labelHolder = NULL; 如果(convertView == NULL){
convertView = el.getView(吹气,父母); 如果(el.isMessage()){ 持有人=新ViewHolder();
holder.messageLayout =(RelativeLayout的)convertView.findViewById(R.id.message_container);
holder.messageContent =(的LinearLayout)convertView.findViewById(R.id.message_content);
holder.bottomIndicator =(的LinearLayout)convertView.findViewById(R.id.bottom_indicators);
holder.dateTextView =(TextView中)convertView.findViewById(R.id.message_date);
holder.timeAgo =(TextView中)convertView.findViewById(R.id.time_ago);
holder.nameTextView =(TextView中)convertView.findViewById(R.id.user_name);
holder.likesCountTextView =(TextView中)convertView.findViewById(R.id.likes_count);
holder.likesLabelImageView =(ImageView的)convertView.findViewById(R.id.likes_label);
holder.spamCountTextView =(TextView中)convertView.findViewById(R.id.spam_count);
holder.spamLabelImageView =(ImageView的)convertView.findViewById(R.id.spam_label);
holder.avatarImageView =(ImageView的)convertView.findViewById(R.id.avatar);
holder.statusImageView =(ImageView的)convertView.findViewById(R.id.delivered_label);
holder.progressBar =(进度)convertView.findViewById(R.id.progress_bar);
holder.errorSign =(ImageView的)convertView.findViewById(R.id.error_sign);
holder.spamSign =(ImageView的)convertView.findViewById(R.id.spam_sign);
holder.bookmarkLabel =(ImageView的)convertView.findViewById(R.id.bookmark_label);
holder.topIndicators =(的LinearLayout)convertView.findViewById(R.id.topIndicators);
convertView.setTag(保持器);
}
如果(el.isNewMessagesLabel()){
convertView = inflater.inflate(R.layout.chat_new_messages_label_layout,父母,假); labelHolder =新NewMessagesLabelHolder();
labelHolder.newMessagesLabel =(TextView中)convertView.findViewById(R.id.new_messages_label);
convertView.setTag(labelHolder);
}
}其他{
如果(el.isMessage()){
支架=(ViewHolder)convertView.getTag();
}
如果(el.isNewMessagesLabel()){
labelHolder =(NewMessagesLabelHolder)convertView.getTag();
}
} 如果(el.isMessage()){
消息currentMessage =(消息)报;
drawMessage(保持,currentMessage,位);
}
如果(el.isNewMessagesLabel()){
NewMessagesLabel messagesLabel =(NewMessagesLabel)埃尔;
drawNewMessagesLabel(labelHolder,messagesLabel);
} 返回convertView;
}私人无效drawMessage(ViewHolder架,留言信息,INT位置){
字符串日期= message.getCreatedAt();
字符串formattedDate = DateHelper.getInstance(上下文).formatDate(YYYY-MM-dd'T'HH:MM:ss.SSS'Z',HH:MM,日期); 字符串username = message.getUserName(); holder.likesLabelImageView.setVisibility(View.GONE);
holder.likesCountTextView.setVisibility(View.GONE);
holder.spamCountTextView.setVisibility(View.GONE);
holder.spamLabelImageView.setVisibility(View.GONE);
holder.bookmarkLabel.setVisibility(View.GONE);
holder.statusImageView.setVisibility(View.GONE);
holder.progressBar.setVisibility(View.GONE);
holder.statusImageView.setVisibility(View.GONE);
holder.errorSign.setVisibility(View.GONE);
holder.spamSign.setVisibility(View.GONE);
holder.timeAgo.setVisibility(View.GONE);
holder.timeAgo.setText();
holder.messageLayout.setAlpha(1F);
holder.topIndicators.setVisibility(View.VISIBLE); 如果(message.isNewDay()){
holder.timeAgo.setVisibility(View.VISIBLE);
字符串的DayDate = DateHelper.getInstance(上下文).formatDate(YYYY-MM-dd'T'HH:MM:ss.SSS'Z',MMMM D,YYYY,日期);
//串的DayDate = getTimeAgo(mHelper.millisFromString(message.getCreatedAt()),背景);
holder.timeAgo.setText(的DayDate);
}其他{
holder.timeAgo.setVisibility(View.GONE);
} holder.avatarImageView.setImageResource(R.drawable.com_facebook_profile_default_icon); 如果(GrouviDatabaseManager.getInstance(上下文).getCurrentUser()。的getId()== message.getUserId()){
的userName = USERNAME_ME;
如果(message.getStatus()= NULL和放大器;!&安培; message.getStatus()等于(Message.MSG_STATUS_SEND)){
holder.statusImageView.setBackgroundResource(R.drawable.icon_chat_message_delivered);
holder.statusImageView.setVisibility(View.VISIBLE);
}
如果(message.getStatus()= NULL和放大器;!&安培; message.getStatus()等于(Message.MSG_STATUS_SENDING)){
holder.progressBar.setVisibility(View.VISIBLE);
}
如果(message.getStatus()= NULL和放大器;!&安培; message.getStatus()等于(Message.MSG_STATUS_LOCAL)){
holder.statusImageView.setBackgroundResource(R.drawable.icon_chat_message_delivered);
holder.statusImageView.setVisibility(View.VISIBLE);
}
如果(message.getStatus()== NULL || message.getStatus()。等于(Message.MSG_STATUS_ERROR)){
holder.messageLayout.setAlpha(0.4f);
holder.errorSign.setVisibility(View.VISIBLE);
}
}
如果(message.getSpamCount()大于0&放大器;&放大器; CURRENTMODE == MODE_NORMAL){
holder.messageLayout.setAlpha(0.4f);
holder.spamSign.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
holder.messageContent.setLayoutParams(PARAMS); holder.spamCountTextView.setText(Integer.toString(message.getSpamCount()));
holder.spamCountTextView.setVisibility(View.VISIBLE);
holder.spamLabelImageView.setVisibility(View.VISIBLE);
}其他{
holder.messageLayout.setAlpha(1F);
holder.spamSign.setVisibility(View.GONE);
RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
holder.messageContent.setLayoutParams(PARAMS);
} 如果(CURRENTMODE == MODE_REPORT){
串TIMEAGO = getTimeAgo(mHelper.millisFromString(message.getCreatedAt()));
holder.timeAgo.setText(TIMEAGO);
holder.timeAgo.setVisibility(View.VISIBLE);
} 如果(message.getUserAvatar()=空&放大器;!&放大器;!message.getUserAvatar()的isEmpty()){
尝试{
毕加索
。随着(上下文)
.load(message.getUserAvatar())
.error(R.drawable.com_facebook_profile_default_icon)
.placeholder(R.drawable.com_facebook_profile_default_icon)
.into(holder.avatarImageView);
}赶上(例外五){
Log.e(主,e.getMessage(),E);
}
}
colorUsername(持有人,信息,用户名);
holder.nameTextView.setText(用户名);
holder.dateTextView.setText(formattedDate); 如果(message.getLikesCount()大于0){
holder.likesCountTextView.setText(Integer.toString(message.getLikesCount()));
holder.likesCountTextView.setVisibility(View.VISIBLE);
holder.likesLabelImageView.setVisibility(View.VISIBLE);
} 如果(message.isBookmarked()){
holder.bookmarkLabel.setVisibility(View.VISIBLE);
} 清单< MessageComponent> messageComponentList;
messageComponentList = message.getMessageComponents(); drawMessageContent(架,messageComponentList,消息); holder.nameTextView.setTag(位置);
holder.avatarImageView.setTag(位置);
holder.nameTextView.setOnClickListener(userClickListener);
holder.avatarImageView.setOnClickListener(userClickListener); //挂空onLingClickListener显示上下文菜单时
//长按一下整个邮件
holder.nameTextView.setOnLongClickListener(longClickListener);
holder.avatarImageView.setOnLongClickListener(longClickListener);
}私人无效colorUsername(ViewHolder架,留言消息,字符串username){
字符串randomColor = prevColor;
如果(userName.equals(USERNAME_ME)){
randomColor =#000000;
}否则如果(message.getUserId()!= prevMsgUserID){
做{
。String []数组= context.getResources()getStringArray(R.array.username_colors);
randomColor =阵列[新的随机()nextInt(array.length)];
}而(prevColor.equals(randomColor)); }
holder.nameTextView.setTextColor(Color.parseColor(randomColor));
prevColor = randomColor;
prevMsgUserID = message.getUserId();
}
您应该保持颜色的数据类,例如在Message类,或保留在地图适配器,映射信息给颜色。然后随机创建颜色,当它不$在地图p $ psent。或者使用的颜色,如果美元在地图p $ psent。使用地图比保持在数据类中,当颜色不相关的信息,当它不是一个消息属性,所以在这种情况下,一个很好的解决方案是很好的解决方案一个保持它在适配器类,在地图,例如:
私人地图<消息,字符串> colorsMap =新的HashMap<消息,字符串>();私人无效colorUsername(ViewHolder架,留言消息,字符串username){
串randomColor;
如果(colorsMap.contains(消息)){
randomColor = colorsMap.get(消息);
}
其他{
如果(userName.equals(USERNAME_ME)){
randomColor =#000000;
}否则如果(message.getUserId()!= prevMsgUserID){
做{
。String []数组= context.getResources()getStringArray(R.array.username_colors);
randomColor =阵列[新的随机()nextInt(array.length)];
}而(prevColor.equals(randomColor)); }
colorsMap.put(消息,randomColor);
}
holder.nameTextView.setTextColor(Color.parseColor(randomColor));
}
I have chat appIn custom array adapter. I set random colors to usernames for each message with this code:
String[] array = context.getResources().getStringArray(R.array.username_colors);
randomColor = array[new Random().nextInt(array.length)];
nameTextView.setTextColor(Color.parseColor(randomColor));
When new message arrive I add it to listview, and call adapter.notifyDataSetChanged();This causes recoloring existing usernames in messages each time.How can I prevent recoloring existing items in listview when calling adapter.notifyDataSetChanged();
Edit 1
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ChatMessageElement el = list.get(position);
ViewHolder holder = null;
NewMessagesLabelHolder labelHolder = null;
if (convertView == null) {
convertView = el.getView(inflater, parent);
if (el.isMessage()) {
holder = new ViewHolder();
holder.messageLayout = (RelativeLayout) convertView.findViewById(R.id.message_container);
holder.messageContent = (LinearLayout) convertView.findViewById(R.id.message_content);
holder.bottomIndicator = (LinearLayout) convertView.findViewById(R.id.bottom_indicators);
holder.dateTextView = (TextView) convertView.findViewById(R.id.message_date);
holder.timeAgo = (TextView) convertView.findViewById(R.id.time_ago);
holder.nameTextView = (TextView) convertView.findViewById(R.id.user_name);
holder.likesCountTextView = (TextView) convertView.findViewById(R.id.likes_count);
holder.likesLabelImageView = (ImageView) convertView.findViewById(R.id.likes_label);
holder.spamCountTextView = (TextView) convertView.findViewById(R.id.spam_count);
holder.spamLabelImageView = (ImageView) convertView.findViewById(R.id.spam_label);
holder.avatarImageView = (ImageView) convertView.findViewById(R.id.avatar);
holder.statusImageView = (ImageView) convertView.findViewById(R.id.delivered_label);
holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progress_bar);
holder.errorSign = (ImageView) convertView.findViewById(R.id.error_sign);
holder.spamSign = (ImageView) convertView.findViewById(R.id.spam_sign);
holder.bookmarkLabel = (ImageView) convertView.findViewById(R.id.bookmark_label);
holder.topIndicators = (LinearLayout) convertView.findViewById(R.id.topIndicators);
convertView.setTag(holder);
}
if (el.isNewMessagesLabel()) {
convertView = inflater.inflate(R.layout.chat_new_messages_label_layout, parent, false);
labelHolder = new NewMessagesLabelHolder();
labelHolder.newMessagesLabel = (TextView) convertView.findViewById(R.id.new_messages_label);
convertView.setTag(labelHolder);
}
} else {
if (el.isMessage()) {
holder = (ViewHolder) convertView.getTag();
}
if (el.isNewMessagesLabel()) {
labelHolder = (NewMessagesLabelHolder) convertView.getTag();
}
}
if (el.isMessage()) {
Message currentMessage = (Message) el;
drawMessage(holder, currentMessage, position);
}
if (el.isNewMessagesLabel()) {
NewMessagesLabel messagesLabel = (NewMessagesLabel) el;
drawNewMessagesLabel(labelHolder, messagesLabel);
}
return convertView;
}
private void drawMessage(ViewHolder holder, Message message, int position) {
String date = message.getCreatedAt();
String formattedDate = DateHelper.getInstance(context).formatDate("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "HH:mm", date);
String userName = message.getUserName();
holder.likesLabelImageView.setVisibility(View.GONE);
holder.likesCountTextView.setVisibility(View.GONE);
holder.spamCountTextView.setVisibility(View.GONE);
holder.spamLabelImageView.setVisibility(View.GONE);
holder.bookmarkLabel.setVisibility(View.GONE);
holder.statusImageView.setVisibility(View.GONE);
holder.progressBar.setVisibility(View.GONE);
holder.statusImageView.setVisibility(View.GONE);
holder.errorSign.setVisibility(View.GONE);
holder.spamSign.setVisibility(View.GONE);
holder.timeAgo.setVisibility(View.GONE);
holder.timeAgo.setText("");
holder.messageLayout.setAlpha(1f);
holder.topIndicators.setVisibility(View.VISIBLE);
if (message.isNewDay()) {
holder.timeAgo.setVisibility(View.VISIBLE);
String dayDate = DateHelper.getInstance(context).formatDate("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "MMMM d, yyyy", date);
//String dayDate = getTimeAgo(mHelper.millisFromString(message.getCreatedAt()), context);
holder.timeAgo.setText(dayDate);
}else{
holder.timeAgo.setVisibility(View.GONE);
}
holder.avatarImageView.setImageResource(R.drawable.com_facebook_profile_default_icon);
if (GrouviDatabaseManager.getInstance(context).getCurrentUser().getId() == message.getUserId()) {
userName = USERNAME_ME;
if (message.getStatus() != null && message.getStatus().equals(Message.MSG_STATUS_SEND)) {
holder.statusImageView.setBackgroundResource(R.drawable.icon_chat_message_delivered);
holder.statusImageView.setVisibility(View.VISIBLE);
}
if (message.getStatus() != null && message.getStatus().equals(Message.MSG_STATUS_SENDING)) {
holder.progressBar.setVisibility(View.VISIBLE);
}
if (message.getStatus() != null && message.getStatus().equals(Message.MSG_STATUS_LOCAL)) {
holder.statusImageView.setBackgroundResource(R.drawable.icon_chat_message_delivered);
holder.statusImageView.setVisibility(View.VISIBLE);
}
if (message.getStatus() == null || message.getStatus().equals(Message.MSG_STATUS_ERROR)) {
holder.messageLayout.setAlpha(0.4f);
holder.errorSign.setVisibility(View.VISIBLE);
}
}
if (message.getSpamCount() > 0 && currentMode == MODE_NORMAL) {
holder.messageLayout.setAlpha(0.4f);
holder.spamSign.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
holder.messageContent.setLayoutParams(params);
holder.spamCountTextView.setText(Integer.toString(message.getSpamCount()));
holder.spamCountTextView.setVisibility(View.VISIBLE);
holder.spamLabelImageView.setVisibility(View.VISIBLE);
} else {
holder.messageLayout.setAlpha(1f);
holder.spamSign.setVisibility(View.GONE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
holder.messageContent.setLayoutParams(params);
}
if (currentMode == MODE_REPORT) {
String timeAgo = getTimeAgo(mHelper.millisFromString(message.getCreatedAt()));
holder.timeAgo.setText(timeAgo);
holder.timeAgo.setVisibility(View.VISIBLE);
}
if (message.getUserAvatar() != null && !message.getUserAvatar().isEmpty()) {
try {
Picasso
.with(context)
.load(message.getUserAvatar())
.error(R.drawable.com_facebook_profile_default_icon)
.placeholder(R.drawable.com_facebook_profile_default_icon)
.into(holder.avatarImageView);
} catch (Exception e) {
Log.e("Main", e.getMessage(), e);
}
}
colorUsername(holder, message, userName);
holder.nameTextView.setText(userName);
holder.dateTextView.setText(formattedDate);
if (message.getLikesCount()>0) {
holder.likesCountTextView.setText(Integer.toString(message.getLikesCount()));
holder.likesCountTextView.setVisibility(View.VISIBLE);
holder.likesLabelImageView.setVisibility(View.VISIBLE);
}
if (message.isBookmarked()) {
holder.bookmarkLabel.setVisibility(View.VISIBLE);
}
List<MessageComponent> messageComponentList;
messageComponentList = message.getMessageComponents();
drawMessageContent(holder, messageComponentList, message);
holder.nameTextView.setTag(position);
holder.avatarImageView.setTag(position);
holder.nameTextView.setOnClickListener(userClickListener);
holder.avatarImageView.setOnClickListener(userClickListener);
// hang empty onLingClickListener to display context menu when
// long click on whole message
holder.nameTextView.setOnLongClickListener(longClickListener);
holder.avatarImageView.setOnLongClickListener(longClickListener);
}
private void colorUsername(ViewHolder holder, Message message, String userName) {
String randomColor = prevColor;
if (userName.equals(USERNAME_ME)){
randomColor = "#000000";
}else if (message.getUserId()!=prevMsgUserID) {
do {
String[] array = context.getResources().getStringArray(R.array.username_colors);
randomColor = array[new Random().nextInt(array.length)];
}while (prevColor.equals(randomColor));
}
holder.nameTextView.setTextColor(Color.parseColor(randomColor));
prevColor = randomColor;
prevMsgUserID = message.getUserId();
}
You should keep color in the data class, for example in the Message class, or keep Map in the adapter, that maps Message to Color. Then randomly create a color, when it doesn't present in the map. or use the color, if it present in the map. Using a map is good solution than keeping in the data class, when the color doesn't related to message, when it is not a message property, so in that case, a good solution is a keeping it in the adapter class, in the map, for example
private Map<Message, String> colorsMap = new HashMap<Message, String>();
private void colorUsername(ViewHolder holder, Message message, String userName) {
String randomColor;
if(colorsMap.contains(message)) {
randomColor = colorsMap.get(message);
}
else {
if (userName.equals(USERNAME_ME)){
randomColor = "#000000";
}else if (message.getUserId()!=prevMsgUserID) {
do {
String[] array = context.getResources().getStringArray(R.array.username_colors);
randomColor = array[new Random().nextInt(array.length)];
}while (prevColor.equals(randomColor));
}
colorsMap.put(message, randomColor);
}
holder.nameTextView.setTextColor(Color.parseColor(randomColor));
}
这篇关于在Android中如何prevent更新上notifyDataSetChanged现有项目()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!