gridview的自定义适配器记住格式化选项

时间:2013-03-11 12:37:11

标签: android android-gridview custom-adapter

我在android应用程序中有一些针对gridview的自定义适配器。当我第一次启动应用程序时一切正常,但是当我单击按钮并调用函数refreshCalendar()时,适配器“记住”文本属性(字体:BOLD)。 我检查(列出)calendarCells,没有错误。

public class FCalCellsAdapter extends BaseAdapter{

private static final String TAG = "FCalCellsAdapter";

private Context context;
private LayoutInflater inflater;

private List<FCalCell> calendarCells;

public FCalCellsAdapter(Context context, List<FCalCell> calendarCells){
    this.context = context;
    this.calendarCells = calendarCells;

    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void refreshCalendarCells(List<FCalCell> calendarCells){
    this.calendarCells = calendarCells;
}

@Override
public int getCount() {
    return calendarCells.size();
}

@Override
public Object getItem(int position) {
    return calendarCells.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Holder holder;
    View v = convertView;

    if(convertView == null){
        holder = new Holder();
        v = inflater.inflate(R.layout.gridview_item_cell, null);

        holder.cellBackground = (ImageView) v.findViewById(R.id.cell_background);
        holder.cellDay = (TextView) v.findViewById(R.id.cell_day);
        holder.cellPeriodDay = (TextView) v.findViewById(R.id.cell_period_day);

        v.setTag(holder);
    }else{
        holder = (Holder) v.getTag();
    }

    if(calendarCells.get(position).getDayType() == FCalCell.DAY_TYPE_ACTIVE_MONTH){
        holder.cellBackground.setBackgroundResource(R.drawable.background_cell_white);
    }else if(calendarCells.get(position).getDayType() == FCalCell.DAY_TYPE_TODAY){
        holder.cellBackground.setBackgroundResource(R.drawable.background_cell_today);
        holder.cellDay.setTypeface(Typeface.DEFAULT_BOLD);
    }else{
        holder.cellBackground.setBackgroundResource(R.drawable.background_cell_gray);
    }

    holder.cellDay.setText(calendarCells.get(position).getNumberOfDay());
    holder.cellPeriodDay.setText(String.valueOf(calendarCells.get(position).getPeriodDayNumber()));

    return v;
}

private class Holder{
    private ImageView cellBackground;
    private TextView cellDay;
    private TextView cellPeriodDay;
}

}

refreshCalendar(主要活动)

public void refreshCalendar(){
    List<FCalCell> calendarCells = FCal.getListOfCalendarCells(actualMonth,this);
    adapter.refreshCalendarCells(calendarCells);
    adapter.notifyDataSetChanged();
}

1 个答案:

答案 0 :(得分:0)

您需要在if else语句中的所有情况下设置dayCell的字体。 TextView将被重用,他们会记得大胆。

,如果您的表格大于适合屏幕的大小,您将遇到同样的问题。

这样的事情:

if(calendarCells.get(position).getDayType() == FCalCell.DAY_TYPE_ACTIVE_MONTH){
    holder.cellBackground.setBackgroundResource(R.drawable.background_cell_white);
    holder.cellDay.setTypeface(Typeface.DEFAULT);
}else if(calendarCells.get(position).getDayType() == FCalCell.DAY_TYPE_TODAY){
    holder.cellBackground.setBackgroundResource(R.drawable.background_cell_today);
    holder.cellDay.setTypeface(Typeface.DEFAULT_BOLD);
}else{
    holder.cellBackground.setBackgroundResource(R.drawable.background_cell_gray);
    holder.cellDay.setTypeface(Typeface.DEFAULT);
}