Android的选定项选择器不工作
我有一个列表视图,我想在其中以自定义方式突出显示所选项目。我正在适配器的getView方法中设置每个项属性,包括itemView.setSelectedtrue 主布局按以下方式定义listview:Android的选定项选择器不工作,android,listview,css-selectors,Android,Listview,Css Selectors,我有一个列表视图,我想在其中以自定义方式突出显示所选项目。我正在适配器的getView方法中设置每个项属性,包括itemView.setSelectedtrue 主布局按以下方式定义listview: <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:choiceMode="multipleChoice" andr
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice"
android:listSelector="@drawable/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>
作为一个整体,我不需要listview的特定样式,所以我会保留一个默认样式,但是根据,我们需要一个listview的选择器,以便项选择器工作。无论如何,如果没有列表选择器,问题仍然存在
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_背景选择器:
<?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_背景中的android:state_selected=true选择器更改为,例如,android:state_pressed=true,则选择器开始工作,即如果触摸某个项目,则项目背景将变为白色
所以,我想,选择器或者我选择项目的方式都有错误
我可以通过从Java设置背景或利用可检查状态来编写解决方案,但我想了解并解决选择器当前的问题。提前感谢。尝试此选项可更改所选列表项。将其放入列表视图中
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
}
在项目视图中添加此行如何
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>
谢谢,但这只是我在Java编码问题中提到的不必要的解决方法之一。谢谢,非常有用!这仅适用于API 11+:我的listitem会以这种方式变成全息蓝色,而不是选择器中的颜色。为什么我们不能将相同的选择器也设置到布局?@Joan P.S这会隐藏列表项的内容,例如文本和图标