Android 如何在单击时突出显示ListView多个视图?

Android 如何在单击时突出显示ListView多个视图?,android,listview,view,Android,Listview,View,我想突出显示视图,以便用户可以看到它选择了哪个项目。我试着这样做: View.setBackground(COLOR.RED); 在项目的listView内部,单击listener,它就会工作,但是如果列表被滚动,随机视图就会开始改变背景。如何突出显示它们,并且不丢失listView滚动条上突出显示的项目 注意:我使用的是一个自定义适配器,每行一个imageView和三个textView 谢谢 编辑:很抱歉,我忘了说我希望能够选择多个项目。设置项目布局的背景色?看这里,概念应该是相同的-忽略我

我想突出显示视图,以便用户可以看到它选择了哪个项目。我试着这样做:

View.setBackground(COLOR.RED);
在项目的listView内部,单击listener,它就会工作,但是如果列表被滚动,随机视图就会开始改变背景。如何突出显示它们,并且不丢失listView滚动条上突出显示的项目

注意:我使用的是一个自定义适配器,每行一个imageView和三个textView

谢谢


编辑:很抱歉,我忘了说我希望能够选择多个项目。

设置项目布局的背景色?看这里,概念应该是相同的-忽略我的runnable

一个快速的方法是创建两个自定义样式;在drawable文件夹中,可以为正常状态、悬停状态或按下状态创建样式:

因此,在
。/drawable/
中,您希望创建两个元素:

1)
列表\u bg.xml
文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#db0000"
      android:centerColor="#c50300"
      android:endColor="#b30500"
      android:angle="270" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#db0000"
      android:centerColor="#c50300"
      android:endColor="#b30500"
      android:angle="270" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/list_bg" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/list_bg_hover" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_bg_hover" android:state_pressed="false" android:state_selected="true"/>

</selector>
3)
列表选择器.xml
文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#db0000"
      android:centerColor="#c50300"
      android:endColor="#b30500"
      android:angle="270" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#db0000"
      android:centerColor="#c50300"
      android:endColor="#b30500"
      android:angle="270" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/list_bg" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/list_bg_hover" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_bg_hover" android:state_pressed="false" android:state_selected="true"/>

</selector>


现在,为了使用它,您所要做的就是将样式附加到
ListView
行项目的布局中,就像这样,
android:listSelector=“@drawable/list\u selector”
,这应该可以做到。

。在我链接的repo中,看看我如何随着runnable进程更改列表项的背景。您所需要做的就是在
onItemSelected()
完美、简单、直接的解决方案中使用相同类型的代码!我不敢相信它这么简单,我没有想到它!