我已经创建了一个带有所选项目突出显示的 ListView ,并且可以在滚动期间保持突出显示(感谢 SO)...
我的问题/问题是:当我在列表中选择一行时,先前选择的行上的突出显示会按预期消失,但在一个实例中除外:如果我将突出显示的行滚动到列表顶部,但没有完全不在列表中(比如用txt2包裹的数据[看我下面的代码]...部分项目不在可查看列表中,部分项目保留在可查看列表中),然后点击另一行下面,BOTH 行现在突出显示,这是我不想要的!
我是不是做错了什么,或者这是 ListView 滚动期间进行回收的结果?
任何想法/建议/解释将不胜感激。
这是我在适配器的 getView 方法中使用的代码:
public View getView(int position, View convertView, ViewGroup parent)
{
final View row = super.getView(position, convertView, parent);
TextView txt1 = (TextView)row.findViewById(R.id.txt1);
TextView txt2 = (TextView)row.findViewById(R.id.txt2);
TextView txt3 = (TextView)row.findViewById(R.id.txt3);
TextView txt4 = (TextView)row.findViewById(R.id.txt4);
if (position == gv.currentListRecord)
{
txt1.setTextColor(getResources().getColor(R.color.episode_list_font_color_selected));
txt2.setTextColor(getResources().getColor(R.color.episode_list_font_color_selected));
txt3.setTextColor(getResources().getColor(R.color.episode_list_font_color_selected));
txt4.setTextColor(getResources().getColor(R.color.episode_list_font_color_selected));
}
else
{
txt1.setTextColor(getResources().getColor(R.color.genre_list_font_color));
txt2.setTextColor(getResources().getColor(R.color.genre_list_font_color));
txt3.setTextColor(getResources().getColor(R.color.genre_list_font_color));
txt4.setTextColor(getResources().getColor(R.color.genre_list_font_color));
}
return row;
}
最佳答案
您在这里要做的是告诉您的 ListView
应该重绘项目 View
,方法是使用 getView(int, View , View 组)
。这是您更改选定状态的地方。
要实现此目的,请在您的 ListAdapter
上调用 notifyDatasetChanged()
。这将导致为屏幕上的所有项目再次调用 getView(int, View, ViewGroup)
。
假设您的 super
方法有效地回收了 convertView
,那么性能应该不是问题。
关于Android ListView 突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25249132/