【发布时间】:2011-05-17 04:59:21
【问题描述】:
我对更改列表视图中特定行的背景颜色感到困惑,下面是我尝试过的代码。当我滚动列表时,不同的行会突出显示,我想了解这背后的原因。逻辑似乎很简单,但结果却是不可预测的。我应该如何实现这一目标。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.rows_for_layout, null);
holder = new ViewHolder();
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.rated=(ImageView)convertView.findViewById(R.id.rated);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
//selected_position is the position where the list has to be highlighted
if(position==selected_position){
holder.name.setText(elements.get(position).get("name"));
convertView.setBackgroundResource(R.drawable.highlight_this);
holder.rated.setBackgroundResource(R.drawable.star_image);
}else{
holder.name.setText(elements.get(position).get("name"));
}
return convertView;
}//getView ![alt text][1]
【问题讨论】:
-
没有发现代码有任何问题,您何时更改/更新“selected_position”变量?
-
我在 onCreate() 方法中设置变量 'selected_position' 的值,作为参考,请查看此 url 以获取完整代码pastebin.com/ki7q6Wy0
-
您可以在i56.tinypic.com/10x7rsw.png查看图片
标签: android listview colors background