我的解决方案:
在 MyAdapter 类中:
private boolean highlighted = false;
private Integer from, to;
//....
public void setHighlight(int from, int to) {
if ((this.from != null && this.from == from) && (this.to != null && this.to == to)) {
highlighted = false;
this.from = null;
this.to = null;
} else {
highlighted = true;
this.from = from;
this.to = to;
}
}
//...
@Override
public View getView(int pos, View v, ViewGroup vg) {
parsedLine entry = lines.get(pos);
//...
if (highlighted && pos>=from && pos<=to){
v.setBackgroundColor(Color.parseColor(Theme.getHighlightColor()));
}
//...
return v;
}
然后在我想突出显示一些行的地方:
MyAdapter myadapter = (MyAdapter)mylist.getAdapter();
myadapter.setHighlight(from, to);
myadapter.notifyDataSetChanged();
mylist.setSelection(from);