Maarten
Maarten

Reputation: 7318

Setting a ListView item to selected

I have a master-detail ListView set-up, and I want to to change the background for selected items in the master fragment.

I used to have the following code, but this does not allow me to use a full-fledged selector:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        // Highlight selected item
        if (position == selectedPos) {
            view.setBackgroundColor((view.getResources()
            .getColor(R.color.nw_white)));
        } else {
            view.setBackgroundColor((view.getResources()
            .getColor(R.color.nw_grey)));
        }
        return view;
    }

It works fine, but I want to use selectors. (Because I want the items to change background appropriately on focus and click.)

So I changed it to:

            @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        // Highlight selected item
        if (position == selectedPos) {
            view.setSelected(true);
            // view.setBackgroundColor((view.getResources()
            // .getColor(R.color.nw_row_download_master_selected)));
        } else {
            view.setSelected(false);
            // view.setBackgroundColor((view.getResources()
            // .getColor(R.color.nw_row_download_master_normal)));
        }
        Log.d(TAG, "pos " + position + " is selected: "
            + view.isSelected());

        return view;
    }

Where the list rows look like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_row_collection_master"
    android:orientation="horizontal"
    >

   ...
</LinearLayout>

And the selector like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/row_master_detail_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/row_master_detail_focused"
          android:state_focused="true" />
    <item android:drawable="@color/nw_row_master_master_selected"
          android:state_selected="true" />
    <item android:drawable="@color/nw_row_master_normal" />
</selector>

The row responds to click events but not to being selected, even though the logger says that the view, indeed, is selected! What is going on?

Upvotes: 2

Views: 4863

Answers (3)

Philippe Banwarth
Philippe Banwarth

Reputation: 17735

You should use state_activated, not state_selected see https://stackoverflow.com/a/13634298/1283554

  • make your list choosable, e.g. set android:choiceMode="singleChoice" on the ListView

  • use a selector with a 'state_activated="true" state as item background

  • select the item on the ListView with myListView.setItemChecked(position, true)

Note : state_activated is only available since Honeycomb. For older version I think you can't do it using only a selector. You probably have to set the background in getView

Upvotes: 1

Maarten
Maarten

Reputation: 7318

I don't know why the view won't show as selected, even though it is set to selected, but this code should work:

Use ListView.setItemChecked():

mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setItemChecked(position, isActivated);

Selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/row_master_detail_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/row_master_detail_focused"
          android:state_focused="true" />
    <item android:drawable="@color/nw_row_master_master_selected"
          android:state_activated="true" />
    <item android:drawable="@color/nw_row_master_normal" />
</selector>

Because android:state_activated is only available post-Honeycomb, add this to getView():

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                // Highlight selected item
                if (position == selectedPos) {
                    view.setBackgroundColor((view.getResources()
                            .getColor(R.color.nw_row_master_selected)));
                } else {
                    Drawable sel = view.getResources().getDrawable(
                            R.drawable.selector_row_collection_master);
                    view.setBackgroundDrawable(sel);
                }
            }

Upvotes: 4

user2652394
user2652394

Reputation: 1686

You have to set the selector for you item view and remove the selector of your list-view. just set your list-view's selector as @null. Hope this helps.

Upvotes: 1

Related Questions