Android的所选项目选择器不起作用

时间:2013-04-01 10:37:38

标签: android listview css-selectors

我有一个列表视图,我希望以自定义方式突出显示所选项目。我在适配器的getView方法中设置了每个商品属性,包括itemView.setSelected(true)

主要布局以下列方式定义列表视图:

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice"
android:listSelector="@drawable/list_selector" />

(使用选择模式也无济于事。)

list_selector 是一个几乎空的存根:

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

我不需要整体列表视图的特定样式,所以我保留一个默认样式,但根据this answer,我们需要一个listview的选择器才能使项目选择器工作。无论如何,如果没有 list_selector ,问题仍然存在。

listview项目布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:background="@drawable/listitem_background"
    android:orientation="vertical">

并引用以下 listitem_background 选择器:

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

问题是,所选项目没有白色背景。

如果我将 listitem_background 中的android:state_selected="true"选择器更改为android:state_pressed="true",则选择器开始工作,即项目背景变为白色触摸。

所以,我想,选择器或我选择项目的方式都有错误。

我可以通过从Java设置背景或利用可检查状态来编写解决方法,但我想了解并修复选择器的当前问题。提前谢谢。

2 个答案:

答案 0 :(得分:12)

如何在项目视图中添加此行?

android:background="?android:attr/activatedBackgroundIndicator"

例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical" >

<TextView
    android:id="@+id/textViewListRowOption"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" />
</LinearLayout>

然后选择器看起来像这样:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">

<item android:drawable="@drawable/list_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_selected" android:state_activated="true"/>
<item android:drawable="@drawable/list_default"/>

</selector>

列表视图:

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

<ListView
    android:id="@+id/listViewMainFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:listSelector="@drawable/list_item_selector"
    android:choiceMode="singleChoice">
</ListView>

</LinearLayout>

答案 1 :(得分:0)

尝试使用此项更改所选列表项。将其放在列表视图中。

if (currentSelectedView != null && currentSelectedView != v) {
                unhighlightCurrentRow(currentSelectedView);
            }

            currentSelectedView = v;
            highlightCurrentRow(currentSelectedView);



private void unhighlightCurrentRow(View rowView) {
        rowView.setBackgroundColor(Color.TRANSPARENT);//to set a color for un-seclected row
    }

    private void highlightCurrentRow(View rowView) {
        rowView.setBackgroundResource(R.drawable.selector_img);//to set a color for seclected row
    }