Android ListView changes the text color and background color when you click Item. androidlistview

Source: Internet
Author: User

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.









Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.