2

I have set selection color to the listview item

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowTextView" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@drawable/text_selector"
android:textSize="16sp" >

But when the listview loses focus, the text color of the last item is stay on selected state.

enter image description here -> enter image description here

I have tried the following code, but it's not working

View v = mainListView.getSelectedView();
v.setSelected(false);

Is there any way to clear the item selection?

Edit.

After checking with different platforms, the solution is still not working on some devices with Android 4.4.4(ex: Nexus 10), but it can work fine on Android 4.3 devices.

Thanks.

king5201
  • 113
  • 3
  • 9
  • Possible duplicate of https://stackoverflow.com/questions/17751129/deselect-seleted-item-in-listview – dhke Mar 05 '15 at 11:56

3 Answers3

1

But when the listview loses focus, the text color of the last item is stay on selected state.

Please, remove the state_selected from your selector drawable xml and declare some choiceMode for it. My ListView declaration:

<ListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:choiceMode="singleChoice"
    android:listSelector="@drawable/main_list_selector"
    ...

My selector for the list view (main_list_selector.xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/button_focused"/>

    <item android:state_pressed="true" android:drawable="@drawable/button_checked_background"/>
    <item android:state_activated="true" android:drawable="@drawable/button_checked_background"/>
</selector>

And row item layout:

<?xml version="1.0" encoding="utf-8"?>
<com.example.yourapp.ui.checkable.CheckableLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <CheckedTextView
        android:id="@+id/item_text_view"
        ...
        android:background="@drawable/row_item background"/>
</com.example.yourapp.ui.checkable.CheckableLinearLayout>

row_item background:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/button_checked_background"/>
    <item android:drawable="@android:color/transparent"/>
</selector>

As you see, I'm using CheckedTextView with background "row_item background" that has drawable state_checked, plus custon LinearLayout. LinearLayout code:

public class CheckableLinearLayout extends LinearLayout implements Checkable {
    private boolean checked = false;

    public CheckableLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CheckableLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean isChecked() {
        return this.checked;
    }

    @Override
    public void setChecked(boolean checked) {
        this.checked = checked;
        this.setSelected(this.checked);

        final int childsCount = getChildCount();
        for (int i = 0; i < childsCount; ++i) {
            View child = getChildAt(i);
            if (child instanceof Checkable) {
                ((Checkable) child).setChecked(checked);
            }
        }
    }

    @Override
    public void toggle() {
        setChecked(!this.checked);
    }
}

Some of this hidden android logic I've found in this https://stackoverflow.com/a/7425650/2133585 answer.

Community
  • 1
  • 1
Kirill Vashilo
  • 1,559
  • 1
  • 18
  • 27
0

Try this :

v.clearChoices();
v.requestLayout();

EDIT :

According to this link ListView selection remains persistent after exiting choice mode , you can also try :

v.clearChoices();
for (int i = 0; i < v.getCount(); i++)
    v.setItemChecked(i, false);
v.post(new Runnable() {
    @Override
    public void run() {
        v.setChoiceMode(ListView.CHOICE_MODE_NONE);
    }
});
Community
  • 1
  • 1
Bubu
  • 1,533
  • 14
  • 14
0

Because the above solution is still not working on some devices, so I find a solution to solve this problem.

private View mSelectedView; // the view has been selected


// set text color when the list view get focus or not
mainListView.setOnFocusChangeListener(new OnFocusChangeListener() {
    if(hasFocus) {
        if(mSelectedView != null && mSelectedView instanceof TextView)
            ((TextView)mSelectedView).setTextColor(getResources().getColorStateList(R.drawable.text_selector));
    } else {
        mSelectedView = mainListView.getSelectedView();
        if(mSelectedView != null && mSelectedView instanceof TextView)
            ((TextView)mSelectedView).setTextColor(getResources().getColor(R.color.default_color));
    }
});
king5201
  • 113
  • 3
  • 9