简体   繁体   中英

Change ListView item color on click back and forth

The situation is, that I have listview, which the user can update by entering name and pressing button and delete the item with a long-click. What I want to do, is to change the ListView's selected items text color on click.

I have searched quite alot for answer, but I see people making a new "color" directory, adding XML file with colors and then setting it for the ListView, which does not work for me.

I want the item on-click to change the color to grey, and when it's grey, if tapped again, back to black. Is there a way to do this with Java?

If it is necessary, this is my XML code:

<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"/>

Thanks in advance!

可以直接使用'settextcolor'控制颜色,在适配器中,使用hashmap保存颜色设置信息,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM