我的 ListView 快疯了,我不知道为什么。
在这里,在适配器中,我使用 VISIBLE/GONE 方法放置了一个 ListView 部分分隔符。出于测试目的,我将 if 语句设置在位置 0。现在,我希望节分隔符仅在位置 == 0 列表项上方可见,但它变得非常有趣:
起初它看起来不错,但是当我向下滚动 listView 时,我可以看到,部分分隔符不仅在 position == 0 中,而且在随机位置,它们不应该在那里。最重要的是,当我向后滚动时,分隔符被混合到另一个列表项中,所以不是一个分隔符,而是在随机位置有随机数,发生了什么,我应该怎么做才能修复它?
哦,这是我的适配器:
public class ScheduleListAdapter extends BaseAdapter {
Context mContext;
List<ScheduleItem> scheduleItems;
LayoutInflater mLayoutInflater;
String[] weekDays;
public ScheduleListAdapter(Context mContext, List<ScheduleItem> scheduleItems) {
this.mContext = mContext;
this.scheduleItems = scheduleItems;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
weekDays = mContext.getResources().getStringArray(R.array.schedule_days);
}
@Override
public int getCount() {
return scheduleItems.size();
}
@Override
public Object getItem(int position) {
return scheduleItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.list_item_schedule, null);
holder.startTime = (TextView) convertView.findViewById(R.id.textView_scheduleStartTime);
holder.endTime = (TextView) convertView.findViewById(R.id.textView_scheduleEndTime);
holder.module = (TextView) convertView.findViewById(R.id.textView_scheduleModule);
holder.lecturer = (TextView) convertView.findViewById(R.id.textView_scheduleLecturer);
holder.moduleType = (TextView) convertView.findViewById(R.id.textView_scheduleModuleType);
holder.room = (TextView) convertView.findViewById(R.id.textView_scheduleRoom);
holder.daySeparator = (TextView) convertView.findViewById(R.id.textView_scheduleDay);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ScheduleItem scheduleItem = scheduleItems.get(position);
if (position == 0) {
switch (scheduleItem.getDayNumber()) {
case 1:
holder.daySeparator.setText(weekDays[0]);
holder.daySeparator.setVisibility(View.VISIBLE);
break;
case 2:
holder.daySeparator.setText(weekDays[1]);
holder.daySeparator.setVisibility(View.VISIBLE);
break;
case 3:
holder.daySeparator.setText(weekDays[2]);
holder.daySeparator.setVisibility(View.VISIBLE);
break;
case 4:
holder.daySeparator.setText(weekDays[3]);
holder.daySeparator.setVisibility(View.VISIBLE);
break;
case 5:
holder.daySeparator.setText(weekDays[4]);
holder.daySeparator.setVisibility(View.VISIBLE);
break;
default:
holder.daySeparator.setVisibility(View.GONE);
}
}
holder.startTime.setText(scheduleItem.getStartTime());
holder.endTime.setText(scheduleItem.getEndTime());
holder.module.setText(scheduleItem.getModule());
holder.lecturer.setText(scheduleItem.getLecturer());
holder.moduleType.setText(scheduleItem.getModuleType());
holder.room.setText(scheduleItem.getRoom());
convertView.setEnabled(false);
convertView.setOnClickListener(null);
return convertView;
}
private static class ViewHolder {
TextView startTime;
TextView endTime;
TextView module;
TextView lecturer;
TextView moduleType;
TextView room;
TextView daySeparator;
}
}
谢谢 ;)