我正在使用 ListViewsetSelector(...)來更改所選項目的背景顏色,雖然顏色確實發(fā)生了變化,但它會選擇一種默認(rèn)顏色,而不是我想要的顏色。我想創(chuàng)建一個 ListView ,一次只允許選擇一個項目。(幾乎)一切正常,但無論我嘗試什么,所選項目總是以默認(rèn)顏色著色。這是我的代碼:列表顯示:<android.support.constraint.ConstraintLayout android:id="@+id/account_picker_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/rounded_corner_button" > <ListView android:id="@+id/account_picker_list" android:layout_width="0dp" android:layout_height="0dp" android:background="@drawable/account_picker_list_corner" android:backgroundTint="@color/white" android:choiceMode="singleChoice" android:listSelector="@drawable/selection_effect" app:layout_constraintBottom_toTopOf="@+id/account_picker_divider2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> </ListView> [...]</android.support.constraint.ConstraintLayout>選擇效果.xml:<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">自定義 ListView 適配器僅實現(xiàn)getView(...)背景顏色,不會以任何方式更改背景顏色。從 中的語句可以看出selection_effect.xml,我已經(jīng)嘗試了 android 讓我使用的所有可能狀態(tài)的組合,不用說,應(yīng)用程序中顯示的實際顏色不在我上面定義的顏色中。我錯過了什么?
1 回答

千巷貓影
TA貢獻(xiàn)1829條經(jīng)驗 獲得超7個贊
添加一個名為 such 的額外屬性isSelected到Account類。
class Account {
? ? ... other attributes
? ? private boolean isSelected;
}
在:getView_AccountPickerViewAdapter
final Account myAccount = getItem(position);
if (myAccount.isSelected()) {
? ? // set selected background
} else {
? ? // set default background
}
當(dāng)onItemClick列表視圖isSelected被點擊的賬戶項目設(shè)置為真時。
添加回答
舉報
0/150
提交
取消