gpt4 book ai didi

android - 我的自定义适配器 (android) 怎么了?

转载 作者:行者123 更新时间:2023-11-30 04:03:39 24 4
gpt4 key购买 nike

我像这样为我的 ListView 制作了一个适配器:

SimpleAdapter adapter = new SimpleAdapter(this.getBaseContext(), listItem, R.layout.list_cell_icon, new String[] { "img", "title", "description" }, new int[] { R.id.img, R.id.title, R.id.description }) {

@Override
public boolean isEnabled (int position) {

if(position == 1 || position == 2) {
return false;
}
return true;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View v = super.getView(position, convertView, parent);

if(position == 1 || position == 2) {

TextView tv = (TextView) v.findViewById(R.id.title);
tv.setTextColor(Color.DKGRAY);
}

return v;
}
};

现在我有两个项目要在列表中禁用,然后我想以深灰色显示文本。我的问题是位置 0 处的行中的文本颜色也更改为深灰色。怎么会这样 ?我错过了什么吗?

最佳答案

你需要添加这个:

TextView tv = (TextView) v.findViewById(R.id.title);    
if(position == 1 || position == 2) {
tv.setTextColor(Color.DKGRAY);
} else {
tv.setTextColor(Color.WHITE);
}

原因是 getView 被多次调用,不一定按特定顺序调用,getView 会回收 View 。因此,您的 TextView 对象很可能总是同一个对象。因此,您必须将颜色设置回 View 。或者,您可以使用一个状态和一个选定项(例如 TextView 上的 setEnabled(false))

关于android - 我的自定义适配器 (android) 怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12088420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com