我的代码中有一个ListView
,其中包含TextView
个孩子,当OnItemClickListener
被解雇时,我会收到一个视图。这个视图是我从ListView中单击的视图。从这一部分,我将转换此视图使其成为TextView。接下来,我将设置此TextView的文本。问题是,当我向上/向下滚动ListView时,一些ListView的子文本也会发生变化。 ListView重绘其子项,其中包含其他子文本的文本。我该如何解决这个问题?
更新:我在OnItemClickListener上收到的视图与其他子视图在某种程度上相同。当我尝试使用equals()方法比较我单击的上一个视图和新单击的视图时,我发现了这一点。滚动列表视图会重绘其子项,但有时会出现与其他子视图相同的子视图,这些视图在列表中不可见。
这是我的OnItemClickListener
messagesContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(adapter.getItemViewType(i)==ChatAdapter.VOICE_MESSAGE){
RelativeLayout rootView = (RelativeLayout) view;
LinearLayout content = (LinearLayout) rootView.getChildAt(0);
LinearLayout contentWithBackground = (LinearLayout) content.getChildAt(1);
LinearLayout voiceContent = (LinearLayout) contentWithBackground.getChildAt(1);
TextView voiceMessage = (TextView) voiceContent.getChildAt(0);
voiceMessage.setText("Sample Text");
}
}
});
这是我的适配器
public class ChatAdapter extends BaseAdapter {
private List<QBChatMessage> chatMessages;
private Context context;
private static final int TXT_MESSAGE = 0;
private static final int IMAGE_MESSAGE = 1;
public static final int VOICE_MESSAGE = 2;
private DisplayImageOptions displayImageOptions;
public ChatAdapter(Context context, List<QBChatMessage> chatMessages){
this.chatMessages = chatMessages;
this.context = context;
initImageLoaderOptions();
}
public void initImageLoaderOptions() {
displayImageOptions = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
.cacheOnDisc(true).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).build();
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getItemViewType(int position){
if(getItem(position).getProperty("fileUID") == null){
return TXT_MESSAGE;
} else {
if (getItem(position).getProperty("type").equals(QBAttachment.PHOTO_TYPE)) {
return IMAGE_MESSAGE;
} else {
return VOICE_MESSAGE;
}
}
}
@Override
public int getCount() {
if(chatMessages!=null){
return chatMessages.size();
} else{
return 0;
}
}
@Override
public QBChatMessage getItem(int position) {
if(chatMessages!=null){
return chatMessages.get(position);
} else{
return null;
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
QBChatMessage chatMessage = getItem(position);
final LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int type = getItemViewType(position);
if (convertView == null) {
if(type == TXT_MESSAGE){
convertView = vi.inflate(R.layout.list_item_message, null);
} else if(type == IMAGE_MESSAGE){
convertView = vi.inflate(R.layout.list_item_message_image, null);
} else{
convertView = vi.inflate(R.layout.list_item_message_voice, null);
}
holder = createViewHolder(convertView, type, position);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
QBUser currentUser = ChatService.getInstance().getCurrentUser();
boolean isOutgoing = chatMessage.getSenderId() != null && chatMessage.getSenderId().equals(currentUser.getId());
setAlignment(holder, isOutgoing, type);
if (chatMessage.getProperty("fullName") != null) {
holder.txtInfo.setText(chatMessage.getProperty("fullName") + ": " + getTimeText(chatMessage));
} else {
holder.txtInfo.setText(getTimeText(chatMessage));
}
if(type == TXT_MESSAGE){
holder.txtMessage.setText(chatMessage.getBody());
} else if (type == IMAGE_MESSAGE){
Log.i("Loading", "Loading");
ImageLoader.getInstance().displayImage(AttachmentConstants.URL_S3+chatMessage.getProperty("fileUID"), holder.imageMessage, displayImageOptions);
}
ImageLoader.getInstance().displayImage(AttachmentConstants.URL_FACEBOOK_OPEN+chatMessage.getProperty("social_picture")+ AttachmentConstants.URL_FACEBOOK_CLOSING, holder.socialPhoto, displayImageOptions);
return convertView;
}
private ViewHolder createViewHolder(View v, int type, final int position) {
final ViewHolder holder = new ViewHolder();
holder.content = (LinearLayout) v.findViewById(R.id.content);
holder.contentWithBG = (LinearLayout) v.findViewById(R.id.contentWithBackground);
holder.txtInfo = (TextView) v.findViewById(R.id.txtInfo);
holder.socialPhoto = (ImageView) v.findViewById(R.id.social_photo);
if(type == TXT_MESSAGE){
holder.txtMessage = (TextView) v.findViewById(R.id.txtMessage);
} else if(type == IMAGE_MESSAGE) {
holder.imageMessage = (ImageView) v.findViewById(R.id.imageMessage);
} else{
holder.voiceMessage = (TextView) v.findViewById(R.id.voiceMessage);
holder.voiceSeekBar = (SeekBar) v.findViewById(R.id.voiceSeekBar);
holder.voiceSeekBar.setEnabled(false);
}
return holder;
}
private String getTimeText(QBChatMessage message) {
return TimeUtils.millisToLongDHMS(message.getDateSent() * 1000);
}
private static class ViewHolder {
public TextView txtMessage;
public TextView txtInfo;
public LinearLayout content;
public LinearLayout contentWithBG;
public ImageView socialPhoto;
public ImageView imageMessage;
public TextView voiceMessage;
public SeekBar voiceSeekBar;
}
}
这是list_item_message_voice.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="20sp"
android:layout_gravity="right"
android:textSize="10sp"
android:textColor="@android:color/secondary_text_dark" />
<LinearLayout
android:id="@+id/contentWithBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/incoming_message_bg"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:orientation="horizontal">
<ImageView
android:layout_margin="10dp"
android:id="@+id/social_photo"
android:layout_width="33.33dp"
android:layout_height="33.33dp" />
<LinearLayout
android:id="@+id/voiceContent"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="Play"
android:id="@+id/voiceMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"/>
<SeekBar
android:id="@+id/voiceSeekBar"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
由于您更改了TextView
中的文本,但未更改适配器的chatMessages
列表中的文本,因此文本发生了变化。
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(adapter.getItemViewType(i)==ChatAdapter.VOICE_MESSAGE){
//Your code.....
voiceMessage.setText("Sample Text");
//Update the chat messages in the list.
}
}
答案 1 :(得分:0)
你可以这样做:
messagesContainer.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
TextView voiceMessage = (TextView)view.findViewById(R.id.txtMessage);
if(!voiceMessage.getText().toString().equals(""))
voiceMessage.setText("Sample Text");
}
});
尝试在适配器中替换它
else
{
if(!holder.txtInfo.getText().toString().equals(""))
holder.txtInfo.setText(getTimeText(chatMessage));
}
答案 2 :(得分:0)
if(convertView == null){
convertView = vi.inflate(R.layout.list_item_message, null);
}
// You can set the data here...
return covertView;