Android ListView的item背景色設(shè)置和item點(diǎn)擊無響應(yīng)的解決方法
下面講解以下在使用listview時(shí)最常見的幾個(gè)問題。
1.如何改變item的背景色和按下顏色
listview默認(rèn)情況下,item的背景色是黑色,在用戶點(diǎn)擊時(shí)是黃色的。如果需要修改為自定義的背景顏色,一般情況下有三種方法:
1)設(shè)置listSelector
2)在布局文件中設(shè)置item的background
3)在adapter的getview中設(shè)置
這三種方法都能達(dá)到改變item默認(rèn)的背景色和按下顏色,下面來分別講解,但是在這之前需要先寫好selector.xml文件;
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/light_blue"></item>
<item android:state_pressed="false" android:drawable="@color/sgray"></item>
</selector>
在改變button或者listview的item默認(rèn)背景色,就可以用到selector。drawable可以設(shè)置為色彩資源,也可以設(shè)置為圖片資源。
1)設(shè)置listview的listSelector
<ListView
android:id="@+id/history_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#565C5D"
android:dividerHeight="3dp"
android:listSelector="@drawable/selector"
android:cacheColorHint="@android:color/transparent">
</ListView>
2)在listitem的布局文件中設(shè)置background屬性,下面是listitem的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/selector">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歷史記錄"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_centerInParent="true">
</TextView>
</RelativeLayout>
3)在adapter的getView方法中設(shè)置
if(convertView ==null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.listitem, null);
}
convertView.setBackgroundResource(R.drawable.selector);
上述方法都能達(dá)到同樣的效果,就是改變item默認(rèn)的背景色和點(diǎn)擊時(shí)的背景顏色,第三種方法最靈活,如果listview的奇數(shù)行和偶數(shù)行需要設(shè)置為不同的selector,只能用第三種方法。
2.包含button,checkbox等控件時(shí)點(diǎn)擊無響應(yīng)問題。
如果listitem里面包括button或者checkbox等控件,默認(rèn)情況下listitem會失去焦點(diǎn),導(dǎo)致無法響應(yīng)item的事件,最常用的解決辦法是在listitem的布局文件中設(shè)置descendantFocusability屬性。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="@+id/history_item_checkbt"
android:layout_height="30dp"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:checked="false"
>
</CheckBox>
<ImageView
android:id="@+id/history_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/history_item_checkbt"
android:background="@drawable/item_icon">
</ImageView>
<Button
android:id="@+id/history_item_edit_bt"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="編輯"
android:textColor="#ffffff"
android:textSize="14sp"
android:background="@drawable/button_bg">
</Button>
<TextView
android:id="@+id/history_item_time_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#565C5D"
android:textSize="14sp"
android:text="10-01 10:20"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@id/history_item_edit_bt">
</TextView>
<TextView
android:id="@+id/history_item_title_tv"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_centerVertical="true"
android:textColor="#565C5D"
android:textSize="14sp"
android:text="xxxxxxxxXXXXXXXXXXXXXXXX"
android:ellipsize="end"
android:maxLines="1"
android:layout_toRightOf="@id/history_item_image"
android:layout_toLeftOf="@id/history_item_time_tv"
android:layout_marginLeft="3dp">
</TextView>
</RelativeLayout>
- android的ListView點(diǎn)擊item使item展開的做法的實(shí)現(xiàn)代碼
- Android編程實(shí)現(xiàn)Listview點(diǎn)擊展開和隱藏的方法
- Android編程實(shí)現(xiàn)ListView中item部分區(qū)域添加點(diǎn)擊事件功能
- Android開發(fā)實(shí)現(xiàn)ListView點(diǎn)擊item改變顏色功能示例
- Android 實(shí)現(xiàn)ListView的點(diǎn)擊變色的實(shí)例
- Android ListView的Item點(diǎn)擊效果的定制
- Android實(shí)現(xiàn)為ListView同時(shí)設(shè)置點(diǎn)擊時(shí)的背景和點(diǎn)擊松手之后的背景
- Android開發(fā)之ListView實(shí)現(xiàn)Item局部刷新
- android ListView內(nèi)數(shù)據(jù)的動態(tài)添加與刪除實(shí)例代碼
- android ListView和GridView拖拽移位實(shí)現(xiàn)代碼
- Android開發(fā)實(shí)現(xiàn)ListView點(diǎn)擊展開收起效果示例
相關(guān)文章
Flutter進(jìn)階之實(shí)現(xiàn)動畫效果(六)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動畫效果第六篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android實(shí)現(xiàn)點(diǎn)擊兩次返回鍵退出
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊兩次返回鍵退出的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android動態(tài)權(quán)限申請實(shí)現(xiàn)步驟分解
對于一些危險(xiǎn)權(quán)限在AndroidManifest清單文件中申請之后,還需要得到用戶的許可并打開,才算是真正的開啟了這個(gè)權(quán)限。所以可以使用動態(tài)申請權(quán)限,對于某個(gè)功能,如果需要開啟某個(gè)權(quán)限,在用戶使用它之前,彈窗提示用戶是否要開啟這個(gè)權(quán)限2023-04-04
Android JNI 調(diào)用時(shí)緩存字段和方法ID示例
這篇文章主要介紹了Android JNI 調(diào)用時(shí)緩存字段和方法ID示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
詳解Android 全局彈出對話框SYSTEM_ALERT_WINDOW權(quán)限
本篇文章主要介紹了詳解Android 全局彈出對話框SYSTEM_ALERT_WINDOW權(quán)限,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Android 媒體庫數(shù)據(jù)更新方法總結(jié)
這篇文章主要介紹了Android 媒體庫數(shù)據(jù)更新方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04
android 獲取視頻,圖片縮略圖的具體實(shí)現(xiàn)
android 獲取視頻,圖片縮略圖的具體實(shí)現(xiàn),需要的朋友可以參考一下2013-06-06
Android實(shí)現(xiàn)左滑退出Activity的完美封裝
這篇文章主要介紹了Android實(shí)現(xiàn)左滑退出Activity的完美封裝,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

