当我滚动使用自定义适配器的ListView过快上下,getView()开始表现奇怪。为什么? [英] When I scroll a listview with a custom adapter too fast up and down, getView() starts behaving oddly. Why?

查看:115
本文介绍了当我滚动使用自定义适配器的ListView过快上下,getView()开始表现奇怪。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理约15串定制arrayadapter列表视图。每一行交替的样式(标签和值之间的那些标签 - 例如第1行可以是电子邮件地址和第2行是实际的电子邮件地址)。我改变了每一行的风格交替这样的一个ArrayAdapter的getView()方法。因此,如果在当前位置上的产品标签,我会从默认的行样式改变风格(这是什么价值观已经应用到它们)。当列表视图首次加载,造型是完美的,只是如何,我希望它是。如果我滚动列表慢慢地向上或向下,它保持这样。不过,如果我滚动列表快速上下,的值的行造型开始变化到的标签的,直到所有的行的有标签行的造型。没有人知道为什么会是这样吗?我用其他列表视图定义适配器的应用程序没有问题,是这样的。

编辑:发现它也改变所有行的对纵向状态>横向变化的标签样式。不上landscape->人像变化做到这一点。下面是我使用的适配器。我失去了一些东西?

 公共类DetailsAdapter扩展ArrayAdapter<字符串> {

私人TextView的文本= NULL;
私人字符串项= NULL;

公共DetailsAdapter(上下文的背景下,INT资源,INT textViewResourceId,字符串[]对象){
    超(背景下,资源,textViewResourceId,对象);
}

@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    文=(TextView的)super.getView(位置,convertView,父母);
    项目=的getItem(位置);
    如果(item.equals(姓名)|| item.equals(手机)|| item.equals(家)|| item.equals(电子邮件)|| item.equals(地址) ){
        text.setBackgroundColor(0xFF575757);
        text.setTextSize(15);
        text.setTypeface(NULL,Typeface.BOLD);
        text.setPadding(8,5,0,5);
    } 其他 {
        text.setPadding(15,15,0,15);
    }
    返回文本;
}

@覆盖
公共布尔的IsEnabled(INT位置){
    项目=的getItem(位置);
    如果(item.equals(姓名)|| item.equals(手机)|| item.equals(家)|| item.equals(电子邮件)|| item.equals(地址) ){
        返回false;
    } 其他 {
        返回true;
    }
}
}
 

解决方案

Android的重用看法相当积极,而且很可能是被用作电子邮件地址列的视图上的行的应该显示标签得到重用,反之亦然。

这样一来,你不能依赖于默认值。设置你的填充,字体,文字大小和背景颜色在所有情况下:

 如果(item.equals(姓名)|| item.equals(手机)|| item.equals(家)|| item.equals(电邮)|| item.equals(地址)){
    text.setBackgroundColor(0xFF575757);
    text.setTextSize(15);
    text.setTypeface(NULL,Typeface.BOLD);
    text.setPadding(8,5,0,5);
} 其他 {
    text.setBackgroundColor(DEFAULT_BACKGROUND);
    text.setTextSize(DEFAULT_TEXT_SIZE);
    text.setTypeface(NULL,DEFAULT_TYPEFACE);
    text.setPadding(15,15,0,15);
}
 

I have a listview with a custom arrayadapter that handles about 15 strings. The style of each row alternates (between labels and values for those labels--for example row 1 could be "email address" and row 2 would be the actual email address). I'm changing the style of each row to alternate like this in the arrayadapter's getView() method. So if the item at the current position is a label, I'll change the styling from the default row style (which is what the values have applied to them). When the listview first loads, the styling is perfect and just how I want it to be. If I scroll the list slowly up or down, it stays that way. However, if I scroll the list fast up and down, the styling of the value rows starts changing to that of the label ones until all of the rows have the styling of a label row. Does anyone know why this would be happening? I've used custom adapters on other listviews in the app with no problems like this.

Edit: Found out that it also changes all of the rows to the label styling on portrait->landscape orientation changes. Doesn't do this on landscape->portrait changes. Below is the adapter I'm using. Am I missing something?

public class DetailsAdapter extends ArrayAdapter<String> {

private TextView text = null;
private String item = null;

public DetailsAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
    super(context, resource, textViewResourceId, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    text = (TextView) super.getView(position, convertView, parent);
    item = getItem(position);
    if (item.equals("Name") || item.equals("Mobile") || item.equals("Home") || item.equals("Email") || item.equals("Address")) {
        text.setBackgroundColor(0xFF575757);
        text.setTextSize(15);
        text.setTypeface(null, Typeface.BOLD);
        text.setPadding(8, 5, 0, 5);
    } else {
        text.setPadding(15, 15, 0, 15);
    }
    return text;
}

@Override
public boolean isEnabled(int position) {
    item = getItem(position);
    if (item.equals("Name") || item.equals("Mobile") || item.equals("Home") || item.equals("Email") || item.equals("Address")) {
        return false;
    } else {
        return true;
    }
}
}

解决方案

Android reuses views fairly aggressively, and it is quite possible that a view that was used as an email address row gets reused on a row that's supposed to display a label, and vice-versa.

As a result, you cannot rely on "default" values. Set your padding, typeface, text size and background color in all cases:

if (item.equals("Name") || item.equals("Mobile") || item.equals("Home") || item.equals("Email") || item.equals("Address")) {
    text.setBackgroundColor(0xFF575757);
    text.setTextSize(15);
    text.setTypeface(null, Typeface.BOLD);
    text.setPadding(8, 5, 0, 5);
} else {
    text.setBackgroundColor(DEFAULT_BACKGROUND);
    text.setTextSize(DEFAULT_TEXT_SIZE);
    text.setTypeface(null, DEFAULT_TYPEFACE);
    text.setPadding(15, 15, 0, 15);
}

这篇关于当我滚动使用自定义适配器的ListView过快上下,getView()开始表现奇怪。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆