ListView部分分隔符跳跃和相乘

时间:2014-07-24 02:21:31

标签: android listview android-listview listview-adapter

我的ListView很疯狂,我不知道为什么。

这里,在适配器中我将ListView部分分隔符与VISIBLE / GONE方法放在一起。为了测试目的,我在位置0设置if语句。现在,我希望区段分隔符在位置== 0列表项之上是可见的,但它真的很有趣:

起初它看起来不错,但是当我向下滚动listView时我可以看到,部分分隔符不只是在位置== 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;
}

}

谢谢;)

1 个答案:

答案 0 :(得分:0)

我在程序中遇到了类似的问题。所以我添加了一个 else 来重置任何设置,如设置颜色,背景或你的情况下的分隔线。

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);
    }
}else{//add code here to reset whatever you set
     holder.daySeparator.setVisibility(View.GONE);
}