I have a ListView with custom layout for its items and need to specify the separate background color for checked items using selector. Here is selector for odd items:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@android:color/black" />
<item android:state_checked="true" android:drawable="@android:color/black" />
<item android:state_selected="true" android:drawable="@color/red" />
<item android:drawable="@android:color/white" />
</selector>
and here is selector for even items:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@android:color/black" />
<item android:state_checked="true" android:drawable="@android:color/black" />
<item android:state_selected="true" android:drawable="@color/red" />
<item android:drawable="@color/gray" />
</selector>
ListView item template looks like this:
<?xml version="1.0" encoding="utf-8"?>
<com.androidhub.core.CheckableLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="40dip">
<TextView
android:text="TextView"
android:id="@+id/textView1"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:textColor="@color/list_item"></TextView>
</com.androidhub.core.CheckableLinearLayout>
The root element of list item is CheckableLinearLayout: http://paste.org/pastebin/view/28711
ListView has single choice mode (I also tried multiple choice mode but it also does not work for me)
public class MainActivity extends ListActivity {
List<String> items = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
items.add("test 1");
items.add("test 2");
items.add("test 3");
items.add("test 4");
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setAdapter(new TestAdapter(this, items));
getListView().setBackgroundColor(getResources().
getColor(android.R.color.white));
}
}
In adapter for this ListView I call setChecked() method of CheckableLinearLayout but it does not change its color:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View v = convertView;
if(v == null) {
v = inflater.inflate(R.layout.list_item, null);
}
if(position % 2 != 0) {
v.setBackgroundResource(R.drawable.list_item_even);
}
else {
v.setBackgroundResource(R.drawable.list_item_odd);
}
if(parent instanceof ListView) {
ListView lv = (ListView)parent;
if(v instanceof CheckableLinearLayout) {
CheckableLinearLayout cll = (CheckableLinearLayout)v;
cll.setChecked(lv.isItemChecked(position));
}
}
return v;
}
What I'm doing wrong? I can see that state_pressed item from selector works fine but state_checked does not. What will be the right approach to setup state_checked background from XML, not from code?
I think you are looking for this attribute:
android:duplicateParentState="true"
Try adding it to your CheckableLinearLayout tag in your cell layout.
The key press/release is the only event that is propagated to the child views of a ViewGroup, so each view will manage it's own state unless you explicitly tell it to use it's parents'.
Also, after this change I think you can just remove the entire block of code that forces the CheckableLinearLayout to be checked:
if(parent instanceof ListView)...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With