【问题标题】:android ListView won't highlight selected rowandroid ListView不会突出显示选定的行
【发布时间】:2013-03-07 18:01:00
【问题描述】:

在我的应用程序中,我从服务器检索用户的播放列表,并通过数据感受到ListView。问题是,ListView 没有突出显示点击的行。为了防止愤怒的 cmets 告诉我我不在这里使用搜索,我尝试过:

  • 将我所在行中的按钮设置为不可聚焦
  • 通过代码和 XML 设置 listSelector(不能同时)
  • 试图在OnItemClickListener 中更改通过View 的背景
  • 使用带有机器人真假参数的 setItemsCanFocus()
  • 将状态为“已选择”和“已按下”的 XML 可绘制对象设置为行布局根目录

在我使用Toast 测试后检测到点击。但我就是不能强制ListView 突出显示选定的行

列表视图

        <ListView
    android:id="@android:id/list"
    android:listSelector="@drawable/list_selected"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/etbg"
    android:choiceMode="singleChoice"
    android:divider="@color/div" >
</ListView>

用作列表行的布局

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<TextView
    android:id="@+id/tvListItemArtist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="14dp"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Small Text"
    android:layout_toRightOf="@+id/tbListPlay"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#FFFFFF" />

<TextView
    android:id="@+id/tvListItemSong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/tvListItemArtist"
    android:layout_centerVertical="true"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/tvListItemArtist"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#FFFFFF" />

<ImageView
    android:id="@+id/img_list_audio_saved"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="50dp"
    android:src="@drawable/attention"
    android:visibility="gone" />

<Button
    android:id="@+id/tbListPlay"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/button_play"
    android:focusable="false"
    android:focusableInTouchMode="false" />

     </RelativeLayout>

有什么建议吗?

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    您已更改 ListSelector:android:listSelector="@drawable/list_selected" 我假设您的自定义可绘制对象不是选择器,它具有不同的状态。请参阅此site 或此question 了解用法

    【讨论】:

    • 您可以在您的 android 文件夹中找到选择器:android-sdk\platforms\android-14\data\res\drawable。然后将其复制到您的项目中并根据您的需要进行更改。
    • 好吧,我尝试删除 listSelector 属性,这意味着应该使用默认属性。没有影响
    【解决方案2】:

    为您的drawables文件夹中的列表视图创建一个选择器,并将列表项背景设置为此drawable。请看下面的代码

    select_button_background.xml

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

    select_button_selectable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <gradient
            android:angle="270"
            android:endColor="#CCCCCC"
            android:startColor="#DDDDDD"
            android:type="linear" />
    
        <corners android:radius="10dp" />
    
        <shape>
            <stroke
                android:width="1dp"
                android:color="#333333" />
        </shape>
    
    </shape>
    

    select_button_selected.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <gradient
            android:angle="270"
            android:endColor="#AAAAAA"
            android:startColor="#BBBBBB"
            android:type="linear" />
    
        <corners android:radius="10dp" />
    
        <shape>
            <stroke
                android:width="1dp"
                android:color="#333333" />
        </shape>
    
    </shape>
    

    【讨论】:

    • 刚刚找到时间转向该项目。您的解决方案有效,谢谢
    【解决方案3】:

    您好,我已经在此问题链接android - identify items in a ListView 中为您发布了答案,您说它对您不起作用,我提供了相同的答案,并且它适用于其他检查此问题Inflate ListView row from OnClickListener in Android?。我希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 2013-01-13
      • 1970-01-01
      • 2014-12-28
      • 2011-10-28
      • 1970-01-01
      相关资源
      最近更新 更多