ListView點擊時Item背景切換

來源:互聯網
上載者:User

mainActivity如下:

package cn.testlistviewcolor;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;//小功能://1 點擊ListView的item時,item的顏色(或者背景圖片)發生變化//2 與此同時,item中的TextView裡的字型顏色發生變化public class MainActivity extends Activity {    private ListView listView;    private ArrayList<HashMap<String, Object>> arrayList;    private HashMap<String, Object> hashMap;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);listView = (ListView) findViewById(R.id.listView);arrayList = new ArrayList<HashMap<String, Object>>();        for(int i=0; i<5; i++) {        hashMap = new HashMap<String, Object>();        hashMap.put("data", "Test" + i);        arrayList.add(hashMap);        }                SimpleAdapter simple =         new SimpleAdapter(this, arrayList, R.layout.list_item, new String[]{"data"},new int[]{R.id.textView});        listView.setAdapter(simple);}}

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <ListView        android:id="@+id/listView"        android:layout_width="fill_parent"        android:layout_height="wrap_content" /></RelativeLayout>

list_item.xml如下:

<?xml version="1.0" encoding="utf-8"?><!-- @drawable/bg_selector1為 item背景圖變換 --><!-- @drawable/bg_selector2為 item背景色變換 --><!-- @drawable/textcolor_selector為點擊和未點擊時TextView上的文字顏色的切換 --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     android:background="@drawable/bg_selector1"    >    <TextView        android:id="@+id/textView"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_margin="5dp"        android:textColor="@drawable/textcolor_selector"     /></LinearLayout>

bg_selector1.xml如下:

<?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/ha" />     <item android:drawable="@drawable/ic_launcher" /> </selector> 

bg_selector2.xml如下:

<?xml version="1.0" encoding="utf-8" ?>  <!--當點擊和平常狀況下顯示不同的顏色 --><selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_pressed="true" android:drawable="@color/red" />     <item android:drawable="@color/green" /> </selector> 

textcolor_selector.xml如下:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_focused="true" android:color="@color/white" /> <!-- focused -->    <item android:state_pressed="true" android:color="@color/white" /> <!-- pressed -->    <item android:state_selected="true" android:color="@color/white" /> <!-- pressed -->    <item android:color="@color/black" /> <!-- default --></selector>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.