我正在尝试在多项选择上设置列表视图项的前景色。但我的问题是它与listview项的background属性折叠。如何在不与背景可绘制选择器冲突的情况下将前景设置为?attr / activatedBackgroundIndicator。我有背景drawable设置为selector_card_bg,显示卡像项目。现在,在选择这些项目时,我希望所选项目在屏幕截图中突出显示。
listview_item:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/selector_card_bg"
android:foreground="?attr/activatedBackgroundIndicator"
android:padding="5dp" >
selector_card_bg:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/card_background_pressed" />
<item android:drawable="@drawable/card_background" />
</selector>
card_background:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#CABBBB"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#4f334e"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
更新 能够通过在适配器的getView中添加代码来实现。但是由于这个原因,getView被调用了很多次。有什么建议可以改善这个吗?
if(mSelectedItemsIds!=null && mSelectedItemsIds.get(position, false))
{
Log.d("tag","if triggered"+mSelectedItemsIds+"position is="+position);
view.setBackgroundColor(Color
.parseColor("#CCCCCC"));
}
else
{
Log.d("tag","else triggered"+mSelectedItemsIds+"position is="+position); // how to restrict this else loop from getting called always
view.setBackgroundResource(R.drawable.selector_card_bg);
}