【问题标题】:How to change background color of selected items in ListView?如何更改 ListView 中所选项目的背景颜色?
【发布时间】:2012-05-04 10:39:36
【问题描述】:

我见过很多类似的问题,每个答案都非常具体,没有直接的答案,或者我找到了展示如何创建选中项目的复选框的教程。 而且我无法从这些代码中理解如何做到这一点。

我正在学习找到 Here 的教程,这就是我的代码看起来只是不同名称的大致情况。

我想有一个多重选择ListView,当一个项目被选中时,背景颜色会改变以标记我选择的项目。

也许我可以使用自定义选择器来完成这个? 我了解常见的方法是保存选定的位置并在 getView 函数中执行某些操作。 我看到人们创建 ViewHolder,但我并不真正理解它与任何东西有什么关系。 有人可以帮帮我吗?

提前致谢, 埃里克

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    好吧,我终于解决了,希望这对某人有所帮助:

    我所做的是创建了一个ArrayList<Integer> 来存储所选项目的所有位置,并在点击时切换背景颜色。

    在我的适配器中我定义:

    public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
    

    使用以下方法:

        public void toggleSelected(Integer position)
    {
        if(selectedIds.contains(position))
        {
            selectedIds.remove(position);
    
    
        }
        else
        {
            selectedIds.add(position);
        }
    }
    

    从 ArrayList 中添加\删除项目

    在我的 getView 方法中:

                if (selectedIds.contains(position)) {
                convertView.setSelected(true);
                convertView.setPressed(true);
                convertView.setBackgroundColor(Color.parseColor("#FF9912"));
            }
            else
            {
                convertView.setSelected(false);
                convertView.setPressed(false);
                convertView.setBackgroundColor(Color.parseColor("#000000"));
            }
    

    这会检查位置是否存储在 ArrayList 中。如果是这样,请将其绘制为选定的。如果不是,则相反。

    剩下的就是 OnItemClick 监听器,我添加了:

        ((YourAdapter)list.getAdapter()).toggleSelected(new Integer(position));
    

    当 YourAdapter 是你的 ListView 的适配器时

    希望这对任何人都有帮助,因为这是一个通用的答案:)

    【讨论】:

    • 我遇到了和你一样的问题,但是在你的解决方案中我无法理解什么是“列表”?
    • 这就是我在OnItemClick事件中命名列表的方式
    【解决方案2】:

    您还可以将以下选择器设置为列表项布局的背景:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:drawable="@color/android:transparent" />
        <item android:drawable="@drawable/listitem_normal" />
    </selector>
    

    来源:ListView item background via custom selector

    【讨论】:

      【解决方案3】:

      有一个简单的 XML 解决方案。下面的语法是 WRT API 15。 我使用了以下列表项模板:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/item_selection">
          <ImageView />
          <.. />
      </LinearLayout>
      

      它指向 res/drawable-hdpi 中的文件 item_selection.xml(在 Android Studio 0.8.14 中):

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      
          <item android:drawable="@android:color/holo_blue_dark" android:state_selected="true" />
      </selector>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-03
        • 1970-01-01
        • 2012-03-10
        • 1970-01-01
        • 1970-01-01
        • 2020-07-03
        • 2021-05-25
        相关资源
        最近更新 更多