Reputation: 722
In my app, i am using a list view to show a list of items that have one text and one image, i have added a list selector attribute to list view so that any list item will be clicked its background color will be changed to a different one.The problem is that when the list item is selected listview's row (that is a text view and a image view) background is not changed however state pressed is working fine.How it will work? Any help will be appreciated
Listview
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:divider="@null"
android:dividerHeight="0dp"
android:listSelector="@drawable/nav_lists_selector"
/>
List Selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@drawable/list_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_selected" android:state_selected="true" />
</selector>
List Pressed
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/clr_list_pressed"/>
</shape>
List Selected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/clr_list_selected"/>
</shape>
Upvotes: 3
Views: 3135
Reputation: 543
Old topic but I run into the same problem. For some strange reason the android:state_selected="true"
and android:state_selected="false"
are reversed for me.
So what finally worked was:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:drawable="@drawable/list_row_bg_selected"/>
<item
android:state_selected="true"
android:drawable="@color/colorWhite"/>
</selector>
So now the only question is, why I have to use those two values reversed.
EDIT:
Think I find why it behaved strangely.
After I added the android:listSelector="@drawable/list_selector"
that I used for the listview to the android:background="@drawable/list_selector"
listview's item view the android:state_selected="true"
and false
work normally as one whould expect in the selector.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal">
<....some Textviews....>
/>
</RelativeLayout>
Upvotes: 2
Reputation: 2471
In onItemclick
of Listview
you can get the position of selected item
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
selectedItem = position;
adapter.notifyDataSetChanged();
}
And in the getView()
method of adapter you can change the color of sleceted item
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);
if (position == selectedItem) {
// Your desired color
}
return view;
}
And using selector try the below code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/pressed" />
<item android:state_focused="false"
android:drawable="@drawable/normal" />
</selector>
Upvotes: 0
Reputation: 419
You need add "normal status" in your List Selector
<item android:drawable="@drawable/normal_list_color_or_image" />
You only was added status selected and pressed, but you don't was added normal status.
"Normal status" is when your component don't have received one action.
Upvotes: 0