gpt4 book ai didi

android - 如何为 ListView 中的每一行设置不同的背景颜色?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:58 27 4
gpt4 key购买 nike

我想在 ListView 的每一行设置不同的背景颜色?我为 listview 使用了自定义适配器.它应该在 Activity 加载时出现。静态不同颜色的行。

最佳答案

getView(...) 方法中

if (position == 0) {
view.setBackgroundResource(R.drawable.bg_list_even);
} else if (position == 1) {
view.setBackgroundResource(R.drawable.bg_list_odd);
} else...

更新::

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

if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

view = inflater.inflate(R.layout.row, null);

holder = new ViewHolder();
view.setTag(holder);

} else {
holder = (ViewHolder) view.getTag();
}

holder.title = (TextView) view.findViewById(R.id.txttitle);
holder.description = (TextView) view.findViewById(R.id.txtdesc);

holder.title.setText("Title" + position);
holder.description.setText("Desc" + position);

//here set your color as per position

if (position == 0) {
view.setBackgroundResource(R.drawable.bg_list_even);
} else if (position == 1) {
view.setBackgroundResource(R.drawable.bg_list_odd);
}
return view;
}

持有人类别

public class ViewHolder {

public TextView title;
public TextView description;
}

关于android - 如何为 ListView 中的每一行设置不同的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10479108/

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