ListView中的TextView在滚动时更改

时间:2015-03-19 04:23:23

标签: android listview scroll

我有一个自定义适配器,可以显示对话中的消息。现在我想加粗textview如果没有读取消息并以其他方式解开它。但是,当我滚动该适配器的列表视图时,TextViews不保留以前的设置,它们会丢失粗体文本或粗体读取文本。我在SO上尝试了很多解决方案,但到目前为止还没有任何工作。

我已将所有变量放入持有者类:

private class Holder {
    public TextView messageTo, messageContent, date;
    public ImageView imgView;
    public Typeface face, bold;
    public int boldText, normalText;
    public int readState;
}

在getView中我在该位置获取消息项并从那里检索readState(省略了一些冗余部分)

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View convertView1 = convertView;
    LayoutInflater vi = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Message message = getItem(position);
    if (convertView1 == null) {
        holder = new Holder();
        convertView1 = vi.inflate(R.layout.list_view_main, parent,
                false);
        holder.messageTo = (TextView) convertView1
                .findViewById(R.id.address);
        holder.messageContent = (TextView) convertView1
                .findViewById(R.id.previewMsg);
        holder.date = (TextView) convertView1.findViewById(R.id.showDate);
        holder.imgView = (ImageView) convertView1
                .findViewById(R.id.quickBadge);
        holder.face = Typeface.createFromAsset(ctx.getAssets(),
                "fonts/helveticaneuelight.ttf");
        holder.bold = Typeface.createFromAsset(ctx.getAssets(),
                "fonts/helveticaneue.ttf");
        holder.boldText = Typeface.BOLD;
        holder.normalText = Typeface.NORMAL;
        holder.readState = getItem(position).readState;
        convertView1.setTag(holder);
    } else {
        holder = (Holder) convertView1.getTag();
    }

    if(holder.readState==0) {
        holder.messageTo.setText(message.messageNumber);
        holder.messageContent.setText(message.messageContent);
        holder.date.setText(message.messageDate);
        holder.messageTo.setTypeface(holder.bold, holder.boldText);
        holder.messageContent.setTypeface(holder.face, holder.boldText);
        holder.date.setTypeface(holder.face, holder.boldText);
    } else {
        holder.messageTo.setText(message.messageNumber);
        holder.messageContent.setText(message.messageContent);
        holder.date.setText(message.messageDate);
        holder.messageTo.setTypeface(holder.bold, holder.normalText);
        holder.messageContent.setTypeface(holder.face, holder.normalText);
        holder.date.setTypeface(holder.face, holder.normalText);
    }
    return convertView1;
}

但它不起作用。任何人都有解决方案吗?

2 个答案:

答案 0 :(得分:1)

试试这个

if(message.readState==0) {
    // keep your other part of the code as it is
    // set font
} else {
   // keep your other code as it is
    // set font
}

你可以摆脱

 holder.readState = getItem(position).readState;

了解它是如何运作的

How ListView's recycling mechanism works

答案 1 :(得分:1)

你可以关注Raghunandan的回答,但是以下回答也可以帮助你:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View convertView1 = convertView;
    LayoutInflater vi = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Message message = getItem(position);
    final Holder holder;
    if (convertView1 == null) {
        holder = new Holder();
        convertView1 = vi.inflate(R.layout.list_view_main, parent,
                false);
        holder.messageTo = (TextView) convertView1
                .findViewById(R.id.address);
        holder.messageContent = (TextView) convertView1
                .findViewById(R.id.previewMsg);
        holder.date = (TextView) convertView1.findViewById(R.id.showDate);
        holder.imgView = (ImageView) convertView1
                .findViewById(R.id.quickBadge);
        holder.face = Typeface.createFromAsset(ctx.getAssets(),
                "fonts/helveticaneuelight.ttf");
        holder.bold = Typeface.createFromAsset(ctx.getAssets(),
                "fonts/helveticaneue.ttf");
        holder.boldText = Typeface.BOLD;
        holder.normalText = Typeface.NORMAL;
        holder.readState = getItem(position).readState;
        convertView1.setTag(holder);
    } else {
        holder = (Holder) convertView1.getTag();
    }

    if(holder.readState==0) {
        holder.messageTo.setText(message.messageNumber);
        holder.messageContent.setText(message.messageContent);
        holder.date.setText(message.messageDate);
        holder.messageTo.setTypeface(holder.bold, holder.boldText);
        holder.messageContent.setTypeface(holder.face, holder.boldText);
        holder.date.setTypeface(holder.face, holder.boldText);
    } else {
        holder.messageTo.setText(message.messageNumber);
        holder.messageContent.setText(message.messageContent);
        holder.date.setText(message.messageDate);
        holder.messageTo.setTypeface(holder.bold, holder.normalText);
        holder.messageContent.setTypeface(holder.face, holder.normalText);
        holder.date.setTypeface(holder.face, holder.normalText);
    }
    return convertView1;
}

只需在getview()方法中创建持有者对象。