0

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?

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78

2 Answers2

1

Have you tried to set focusable and focusableintouchmode to true?

android:focusable="true"
android:focusableInTouchMode="true"

and try to set focusable of the textviews and imageview to false.

  • Yes, i've done that. But another problem occurs. Focus is just for items shown in the screen and all other items in listView which are not shown in the screen, can't get focus. When I use keyboard to go down to the list, focus is not stays in listView.In fact when it reaches to the items located at the bottom of the screen, listView lost the focus and its not scrolls down. – Milad Faridnia Sep 13 '14 at 10:25
0

After a few searches I found that, working with focus in list view in order to change list item background when its selected, is not a good solution. because focus is not stays in list view, after it reaches to the end of screen. In fact, if you set onFocusChange listener for your listView it doesn't scroll down any more (with keyboard of course) and the better solution to solve this issue is setOnItemSelected. If you want more details about the solution, you can take a look at this question:

Programmatically select item ListView in Android

Additional Information: I am developing an app for TV and there is no touch screen.I must handle all keyboard events. So maybe this problem is not occur for common app developers.

Community
  • 1
  • 1
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78