【问题标题】:Android's selector for selected item does not workAndroid 的选定项目选择器不起作用
【发布时间】:2013-03-22 10:24:39
【问题描述】:

我有一个列表视图,我想在其中以自定义方式突出显示选定的项目。我在适配器的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,我们需要一个列表视图的选择器才能使项目选择器工作。无论如何,如果没有 list_selector,问题仍然存在。

列表视图项布局:

<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 设置背景或利用可检查状态来编写解决方法,但我想了解并解决当前选择器的问题。提前致谢。

【问题讨论】:

    标签: android listview css-selectors


    【解决方案1】:

    在您的项目视图中添加这一行怎么样?

    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>
    

    【讨论】:

    • 谢谢,很有帮助!
    • 这仅适用于 API 11+ :(
    • 我的列表项以这种方式变成全息蓝,而不是我的选择器中的颜色。
    • 为什么我们不能也为布局设置相同的选择器?
    • @Joan P.S 这也隐藏了列表项的内容(例如文本和图标)
    【解决方案2】:

    试试这个来改变选中的列表项。把它放在列表视图里面。

    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
        }
    

    【讨论】:

    • 谢谢,但这只是我在问题(Java 编码)中提到的不需要的解决方法之一。
    猜你喜欢
    • 1970-01-01
    • 2021-01-12
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 2020-05-01
    相关资源
    最近更新 更多