我知道有很多类似的问题,但我无法找到我犯的错误,如果有人可以提供帮助的话。 我试图在数组列表中显示值,如聊天格式,但数组列表项在滚动时被放错了
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.chat_row, parent, false);
holder = new ViewHolder();
holder.messageLeft = (TextView) convertView
.findViewById(R.id.leftBox);
holder.messageRight = (TextView) convertView
.findViewById(R.id.rightBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try { if (snd_id.get(position).equalsIgnoreCase(appPref.getData("Uid"))) {
holder.messageLeft.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageLeft.setVisibility(View.VISIBLE);
holder.messageRight.setVisibility(View.GONE);
} else {
holder.messageRight.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageRight.setVisibility(View.VISIBLE);
holder.messageLeft.setVisibility(View.GONE);
}
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
这是视图持有者类
private static class ViewHolder {
public TextView messageLeft;
public TextView messageRight;
}
答案 0 :(得分:4)
只需在holder.messageRight.setVisibility(View.GONE);
声明中加入holder.messageLeft.setVisibility(View.GONE);
和if-else
。
try {
if (snd_id.get(position).equalsIgnoreCase(appPref.getData("Uid"))) {
holder.messageLeft.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageLeft.setVisibility(View.VISIBLE);
holder.messageRight.setVisibility(View.GONE);
} else {
holder.messageRight.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageRight.setVisibility(View.VISIBLE);
holder.messageLeft.setVisibility(View.GONE);
}
让我知道它是否适合你。
答案 1 :(得分:2)
您每次都会findViewById
ViewHolder
。if-else
的设计不是这样的。请改变您的 if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.chat_row, parent, false);
holder = new ViewHolder();
holder.messageLeft = (TextView) convertView
.findViewById(R.id.leftBox);
holder.messageRight = (TextView) convertView
.findViewById(R.id.rightBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
条件并尝试..
convertView.setTag(holder);
并删除
{{1}}
在底部
答案 2 :(得分:2)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.chat_row, parent, false);
holder = new ViewHolder();
holder.messageLeft = (TextView) convertView
.findViewById(R.id.leftBox);
holder.messageRight = (TextView) convertView
.findViewById(R.id.rightBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
if (snd_id.get(position).equalsIgnoreCase(appPref.getData("Uid"))) {
holder.messageLeft.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageLeft.setVisibility(View.VISIBLE);
} else {
holder.messageRight.setText(snd_name.get(position)+":"+"\n"+chat.get(position));
holder.messageRight.setVisibility(View.VISIBLE);
}
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
这是使用视图的有效和正确的方法