我有一个 ListView
的自定义适配器,getView 方法是这样的:
public View getView(int position, View convertView, ViewGroup parent)
{
//final ILSpeechBubble speechBubble;
if(convertView == null)
{
inflater = context.getLayoutInflater();
if(m_msg.get(position).Recieved == 0)
{
convertView = inflater.inflate(R.layout.text_message_sent_row_template, null);
holder = new ViewHolder();
holder.spBubble = (ILSpeechBubble) convertView.findViewById(R.id.iLSpeechBubble1);
holder.imgProfilePic = (ImageView) convertView.findViewById(R.id.imgProfilePic);
holder.imgSend = (ImageView) convertView.findViewById(R.id.imgMessageSent);
holder.imgDeliverd = (ImageView) convertView.findViewById(R.id.imgMessageDeliverd);
holder.pbSending = (ProgressBar) convertView.findViewById(R.id.pbSendingMessage);
holder.spBubble.isChache = false;
holder.Position = position;
convertView.setTag(holder);
m_ViewHolder.add(holder);
}
else
{
convertView = inflater.inflate(R.layout.text_message_recieved_row_template, null);
holder = new ViewHolder();
holder.spBubble = (ILSpeechBubble) convertView.findViewById(R.id.iLSpeechBubble1);
holder.imgProfilePic = (ImageView) convertView.findViewById(R.id.imgProfilePic);
holder.spBubble.isChache = false;
holder.Position = position;
convertView.setTag(holder);
m_ViewHolder.add(holder);
}
}
else
{
holder = (ViewHolder) convertView.getTag();
if(holder.Position == position)
Log.i("View Holder","Position : " + position);
Log.i("View Holder","Position : " + position + " Wrong");
Log.i("View Holder","Position : " + holder.Position + " HolderPos");
//position = holder.Position;
holder = findHolder(position);
if(holder == null)
{
if(m_msg.get(position).Recieved == 0)
{
convertView = inflater.inflate(R.layout.text_message_sent_row_template, null);
holder = new ViewHolder();
holder.spBubble = (ILSpeechBubble) convertView.findViewById(R.id.iLSpeechBubble1);
holder.imgProfilePic = (ImageView) convertView.findViewById(R.id.imgProfilePic);
holder.imgSend = (ImageView) convertView.findViewById(R.id.imgMessageSent);
holder.imgDeliverd = (ImageView) convertView.findViewById(R.id.imgMessageDeliverd);
holder.pbSending = (ProgressBar) convertView.findViewById(R.id.pbSendingMessage);
holder.spBubble.isChache = false;
holder.Position = position;
convertView.setTag(holder);
m_ViewHolder.add(holder);
}
else
{
convertView = inflater.inflate(R.layout.text_message_recieved_row_template, null);
holder = new ViewHolder();
holder.spBubble = (ILSpeechBubble) convertView.findViewById(R.id.iLSpeechBubble1);
holder.imgProfilePic = (ImageView) convertView.findViewById(R.id.imgProfilePic);
holder.spBubble.isChache = false;
holder.Position = position;
convertView.setTag(holder);
m_ViewHolder.add(holder);
}
}
Log.i("View Holder","Position After Find : " + position + " Wrong");
Log.i("View Holder","Position After Find : " + holder.Position + " HolderPos");
holder.spBubble.isChache = true;
//return v;
}
if(m_msg.get(position).Recieved == 0)
{
holder.spBubble.setBubbleDirectionLeft(false);
//speechBubble.setBubbleDirectionLeft(false);
holder.spBubble.setTextAlign(Align.RIGHT);
if (m_msg.get(position).Deliverd == 1)
{
holder.imgDeliverd.setVisibility(View.VISIBLE);
holder.imgDeliverd.setImageResource(R.drawable.message_deliverd);
holder.imgSend.setVisibility(View.GONE);
holder.pbSending.setVisibility(View.GONE);
}
else if (m_msg.get(position).Sent == 1)
{
holder.imgDeliverd.setVisibility(View.GONE);
holder.imgSend.setVisibility(View.VISIBLE);
holder.imgSend.setImageResource(R.drawable.message_sent);
holder.pbSending.setVisibility(View.GONE);
}
}
else
{
holder.spBubble.setBubbleDirectionLeft(true);
holder.spBubble.setTextAlign(Align.RIGHT);
}
CContacts_Info contact = cl_ContactInfo.GetContact(m_ContactILId);
holder.spBubble.setMessageBody(m_msg.get(position).MessageBody);
if(m_msg.get(position).MessageType.equals(AttachType.Emoji.name()))
holder.spBubble.setEmojiList(m_EmojiList);
else if(m_msg.get(position).MessageType.equals(AttachType.Picture.name()))
{
holder.spBubble.setImageSize(128, 128);
holder.spBubble.setImageReadyState(m_msgList.get(position).isImageReady);
if(m_msgList.get(position).isImageReady)
holder.spBubble.setPictureMessageThumbPic(m_msgList.get(position).ThumbPic);
}
holder.spBubble.setVisibility(View.VISIBLE);
holder.spBubble.setTextColor(Color.WHITE);
holder.spBubble.setFontSize(m_Default_FontSize);
holder.spBubble.setMessageId(m_msg.get(position)._id);
if (m_msg.get(position)._id == iMessageId)
{
if (m_msg.get(position).iProgressValue == 100)
m_Downloading = false;
else
m_msg.get(position).iProgressValue += 10;
holder.spBubble.SetProgress(m_msg.get(position).iProgressValue);
}
else
holder.spBubble.SetProgress(100);
//ImageView imgProfilePic = (ImageView) convertView.findViewById(R.id.imgProfilePic);
//holder.imgProfilePic.setPadding(0, holder.spBubble.getHeight(), 0, 0);
if(contact.ProfilePic != null)
{
Bitmap bitmap = BitmapFactory.decodeByteArray(contact.ProfilePic, 0,
contact.ProfilePic.length,m_BitmapOPT);
holder.imgProfilePic.setImageBitmap(bitmap);
}
else
{
holder.imgProfilePic.setImageResource(R.drawable.profile_pic);
}
if (m_isStop)
{
holder.spBubble.setVoiceMessage_Play(false);
holder.spBubble.invalidate();
m_isStop = false;
}
if (m_Playing)
UpdateTimer(holder.spBubble);
// UpdateTimer(holder.spBubble);
holder.spBubble.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String messageType = m_msg.get(holder.Position).MessageType;
if (messageType.equals(AttachType.VoiceMessage.name()))
{
if (m_Playing)
{
stopPlaying();
holder.spBubble.setVoiceMessage_Play(false);
holder.spBubble.invalidate();
}
else
{
String source = holder.spBubble.getAttachSource();
// Toast.makeText(Message_Activity.this, source,
// 1000).show();
startPlaying(source);
ShowNotify(R.string.VoiceMessage_Play_Title);
update_Timer.run();
}
}
else if (messageType.equals(AttachType.Audio.name()))
{
if (holder.spBubble.getProgress() < 100)
return;
String source = holder.spBubble.getAttachSource();
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(source);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
}
else if (messageType.equals(AttachType.Video.name()))
{
if (holder.spBubble.getProgress() < 100)
return;
String source = holder.spBubble.getAttachSource();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(source), "video/*");
startActivity(intent);
}
else if (messageType.equals(AttachType.Picture.name()))
{
String source = holder.spBubble.getAttachSource();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///" + source), "image/*");
startActivity(intent);
}
}
});
holder.spBubble.setOnLongClickListener(new OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
List<Action_Type> actionList = CreatePopUpMenuList_Message(m_msg.get(holder.Position)._id);
ListView lstPopUpAction = (ListView) m_PopUpDialog.findViewById(R.id.listView1);
lstPopUpAction.setAdapter(new Show_Action_Adapter(Message_Activity.this,
actionList));
m_PopUpDialog.show();
return true;
}
});
holder.spBubble.invalidate();
//convertView.setTag(holder);
return convertView;
}
但是滚动后在listView中显示的项目是错误的,有时重复项目,有时显示列表项目的顺序错误。
我在 if/else 之后将 item 的值设置为获取视图,但问题没有解决!
如果我再次创建视图并且不使用缓存 convertView (意味着:
如果(转换视图 == 空
该项目显示正确,但如果滚动到太多,应用程序会因内存过多而崩溃。
我很困惑,不知道我该如何解决。