来回点击更改ListView项目颜色

时间:2017-05-16 17:59:33

标签: java android listview

情况是,我有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"/>

提前致谢!

1 个答案:

答案 0 :(得分:0)

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