I have a custom listView with some views (Like textView and imageView) in it. I want to change the background color of the list item when it gets focus. I have set OnfocusChange for my convert view but it doesn't work. this is my Holder Class:
private class Holder {
ImageView iconImage;
TextView titleText ;
TextView costText ;
}
and getView method:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Item item = item.get(position);
final Holder temp;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
temp = new Holder();
temp.iconImage = (ImageView) convertView
.findViewById(R.id.iconImageView);
temp.titleText = (TextView) convertView
.findViewById(R.id.titleTextView);
temp.costText = (TextView) convertView
.findViewById(R.id.costTextView);
convertView.setTag(temp);
} else {
temp = (Holder) convertView.getTag();
}
temp.titleText.setText(item.getItemTitle());
//this focus listener doesn't work.
convertView.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
temp.titleText.setBackground(context.getResources()
.getDrawable(
R.drawable.green_background));
} else {
temp.titleText.setBackground(context.getResources()
.getDrawable(R.drawable.black_background));
}
}
});
}
how can i set OnFocusChange listener for each list item in my list view?