2017-05-16 80 views
0

的情況是,我有列表視圖,用戶可以通過輸入名字並按下按鈕,更新和長期點擊刪除該項目。我想要做的是在點擊時更改ListView的選定項目文字顏色。上單擊更改ListView項顏色來回

我已經搜索了很多相當的答案,但我看到人們做一個新的「顏色」目錄下,添加XML文件的顏色,然後將它設置爲ListView,它不適合我的工作。

我要上單擊來改變顏色爲灰色的項目,當它是灰色的,如果再次選擇,回黑色。有沒有辦法用Java來做到這一點?

如果有必要,這是我的XML代碼:

<ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" android:id="@+id/groceryListView" 
     android:layout_above="@+id/addNewItem"/> 

<EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:ems="10" 
     android:id="@+id/enterItemEditText" 
     android:maxLength="30" 
     android:hint="@string/enter_your_item_here" android:textColor="#000000" 
     android:layout_below="@+id/groceryListView" android:layout_alignParentStart="true" 
     android:layout_marginStart="36dp"/> 

<android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     app:fabSize="normal" app:srcCompat="@drawable/ic_add_white_48dp" 
     android:id="@+id/addNewItem" 
     android:layout_marginBottom="8dp" android:layout_alignParentBottom="true" 
     android:layout_toEndOf="@+id/enterItemEditText" android:layout_marginStart="20dp"/> 

提前感謝!

回答

0

可直接使用'settextcolor'控制顏色,在適配器中使用hashmap保存顏色設置信息,