Android ListView changes the text color and background color when you click Item. androidlistview
As follows:
List. xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#ffffff" android:paddingTop="10dp" > <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="10dp" android:background="#e6e6e6" /> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView></LinearLayout>
List_item.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="20dp" android:paddingTop="10dp" android:paddingBottom="10dp" android:background="@drawable/listitem_selector" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@drawable/font_selector" android:duplicateParentState="true" android:gravity="center_vertical" android:textSize="18sp" /></LinearLayout>
Listitem_selector:
<? Xml version = "1.0" encoding = "UTF-8"?> <Selector xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Background color without focus --> <item android: drawable = "@ color/unfocused" android: state_window_focused = "false"/> <! -- Background color when the focus is obtained in non-touch mode --> <item android: drawable = "@ color/pressed" android: state_focused = "true" android: state_pressed = "true"/> <! -- Background color when you click in touch mode --> <item android: drawable = "@ color/pressed" android: state_focused = "false" android: state_pressed = "true"/> <! -- Selected background color --> <item android: drawable = "@ color/pressed" android: state_selected = "true"/> <! -- Background color when getting focus --> <item android: drawable = "@ color/pressed" android: state_focused = "true"/> </selector>
Font_selector:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:color="#ff0030" /> <!-- focused --> <item android:state_pressed="true" android:color="#ff0030" /> <!-- pressed --> <item android:state_selected="true" android:color="#ff0030" /> <!-- pressed --> <item android:color="#222222" /> <!-- default --></selector>
Activity:
listView = (ListView) contentView1.findViewById(R.id.list);ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();for (String i : name) {HashMap<String, String> map = new HashMap<String, String>();map.put("data", i);data.add(map);}SimpleAdapter simple = new SimpleAdapter(getActivity(), data,R.layout.layout_popup_item, new String[] { "data" },new int[] { R.id.text1 });listView.setAdapter(simple);
Note:
TextView also adds attributes
Android: duplicateParentState = "true"
In this way, the status of the ParentView will change.