【问题标题】:how to highlight ListView multiple views on click?如何在单击时突出显示 ListView 多个视图?
【发布时间】:2012-12-31 13:20:47
【问题描述】:

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

View.setBackground(COLOR.RED);

在项目点击监听器上的 listView 内部,它可以工作,但如果列表滚动,随机视图开始改变背景。如何突出显示它们并且不会丢失 listView 滚动上突出显示的项目?

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

谢谢

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

【问题讨论】:

    标签: android listview view


    【解决方案1】:

    设置项目布局的背景颜色?看这里https://github.com/cplain/custom-list 概念应该是一样的——忽略我的runnable

    【讨论】:

    • 我想这就是你想要的。在我链接的 repo 中,看看我如何随着可运行的进程更改列表项的背景。您需要做的就是在onItemSelected() 中使用相同类型的代码
    • 完美、简单、直接的解决方案!我不敢相信它是如此简单,我没有考虑它!
    【解决方案2】:

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

    所以在../drawable/ 中你想要制作几个元素:

    1) list_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>
    

    2) list_bg_hover.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>
    

    3) list_selector.xml 文件:

    <?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_selector",这样就可以了。

    【讨论】:

      猜你喜欢
      • 2021-04-02
      • 2011-03-16
      • 2018-12-30
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 2013-02-18
      相关资源
      最近更新 更多