gpt4 book ai didi

android - 更改 ListView 中第一项的背景颜色

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:13 25 4
gpt4 key购买 nike

我的 ListView 有一个自定义适配器:

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



if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);

holder = new DataHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.locationName = (TextView)row.findViewById(R.id.locationName);
holder.locationElevation = (TextView)row.findViewById(R.id.lcoationElevation);
holder.locationDistance = (TextView)row.findViewById(R.id.locationDistance);
row.setTag(holder);
}
else
{
holder = (DataHolder)row.getTag();
}

Data data = gather[position];
holder.locationName.setText(data.locationName);
holder.locationElevation.setText(data.locationElevation);
holder.locationDistance.setText(Double.toString(data.heading));
holder.imgIcon.setImageBitmap(data.icon);



return row;
}

我的 ListView 中填充了项目,我只希望第一个项目具有红色背景色。当我滚动时,所有其他项目都保留了自己的颜色,但第一个项目仍然是红色。有任何想法吗?每次我尝试一些东西时,红色背景都会在我滚动时移动到其他行。

最佳答案

Everytime I try something the red background moves around to the other rows as I scroll.

我猜你没有 else 子句。每个行布局都由适配器重用以节省资源。因此,如果您更改布局中的值,它将在下次回收此特定布局时继续使用。只需添加一条 else 语句即可将回收的 View 返回到默认状态:

if(position == 0)
row.setBackgroundColor(Color.RED);
else
row.setBackgroundColor(0x00000000); // Transparent

(如果背景是特定颜色而不是透明的,则需要更改该值。)

关于android - 更改 ListView 中第一项的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13781849/

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