【问题标题】:Android listview entire list getting selectedAndroid listview整个列表被选中
【发布时间】:2013-04-12 01:56:45
【问题描述】:

我似乎遇到了列表视图的 UI 问题。我有一个列表视图并选择列表中的任何项目,突出显示整个列表视图。选择工作正常,我在监听器中得到了正确的项目。

但问题是,当我选择任何项目时,整个列表视图都会突出显示,因此很难判断选择了哪一行。

这在 Android >3.1 上运行良好 - 目前我正在 2.3 设备上进行测试。

<ListView
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="@dimen/margin_medium"
        android:background="@drawable/border"
        android:listSelector="#99000000"
        android:scrollbarFadeDuration="1000000"
        android:scrollbars="vertical" >
    </ListView>

【问题讨论】:

    标签: android listview


    【解决方案1】:

    我最近遇到了同样的问题,但原因在于 Android

    因此,在res/drawable 文件夹中创建一个具有相似内容的新 XML 文件:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <solid android:color="@color/my_list_item_pressed_color" />
    </shape>
    

    并且在state_pressedListView 选择器中引用创建的drawable(也在res/drawable 文件夹中创建为XML 文件):

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:state_pressed="true" android:drawable="@drawable/list_item_pressed_bg" />
        <item android:drawable="@android:color/transparent" />
    </selector>
    

    并在您的 ListView 中使用此选择器:

    <ListView
        .....
        android:listSelector="@drawable/list_item_selector" />
    

    就是这样。至少适用于 Android 2.2-2.3。

    【讨论】:

    • 谢谢!这些 3.0/4.0 之前的 bug 真的很烦人,因为它们根本没有文档记录!
    • 希望我能在几小时前找到这篇文章。请注意,您也可以通过编程方式执行此操作:ShapeDrawable sd = new ShapeDrawable(new RectShape()); sd.getPaint().setColor(Color.RED); listview.setSelector(sd);
    【解决方案2】:

    如果不想更改选择器,也可以将其设置为列表项布局的背景,而不是在 ListView 的 listSelector 字段中。列表项在被触摸时获得“选中”状态并且颜色显示正确。

    这适用于所有版本的 Android。

    例如:这是您的列表项布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_color">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Your text here" />
    </LinearLayout>
    

    你的列表没有列表选择器:

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    

    【讨论】:

      【解决方案3】:

      我通过删除这一行来解决这个问题:(不知道为什么会起作用)

      android:listSelector="#99000000"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        相关资源
        最近更新 更多