" ", , , . ListView, () (unhighlight) .
public class AutoSelectingListViewAdapter extends ArrayAdapter<Thingy>{
private LayoutInflater inflater;
public boolean shouldHighlight = false;
public int highlightIndex = -1;
public AutoSelectingListViewAdapter(Context context, int resourceId, List<Thingy> objects) {
super(context, resourceId, objects);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ItemHolder holder = null;
if (convertView == null){
holder = new ItemHolder();
convertView = inflater.inflate(R.layout.custom_list_item, null);
holder.clue = (TextView) convertView.findViewById(R.id.custom_list_item_text);
convertView.setTag(holder);
}else{
holder = (ItemHolder) convertView.getTag();
}
if (shouldHighlight && highlightIndex == position){
convertView.setBackgroundColor(0xffb5bfcc);
}else{
convertView.setBackgroundColor(0x00000000);
}
Thingy thingy = this.getItem(position);
holder.clue.setText(thingy.textData);
return convertView;
}
class ItemHolder{
TextView text;
}
}
, , :
targetListView.setSelection(4);
AutoSelectingListViewAdapter myAdapter = (AutoSelectingListViewAdapter) targetListView.getAdapter();
myAdapter.highlightIndex = 4;
myAdapter.shouldHighlight = true;
myAdapter.notifyDataSetChanged();
:
myAdapter.shouldHighlight = false;
myAdapter.notifyDataSetChanged();