繁体   English   中英

来回更改ListView项的颜色

[英]Change ListView item color on click back and forth

情况是,我具有列表视图,用户可以通过输入名称并按按钮来更新它,并长按删除该项目。 我想要做的是单击更改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"/>

提前致谢!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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