zor*_*b76 7 android listview adapter
我有一个列表视图和一个适配器,它将交替的背景颜色设置为列表项("斑马"列表样式):
public View getView(final int position, View convertView, ViewGroup parent) {
int colorPos = position % colors.length;
...
convertView.setBackgroundColor(colors[colorPos]);
return convertView;
}
Run Code Online (Sandbox Code Playgroud)
但是现在,当我使用滚轮选择项目时,或者当我单击某个项目时,选择/单击的原始颜色不会覆盖我的自定义背景(我可以看到我设置的原始颜色下方的原始颜色).
如何设置这些状态的原始颜色?
Uty*_*tyi 20
我认为最简单的方法是创建两个用作背景资源的选择器,在state_selected模式下使用透明颜色:(res/drawable/alterselector1.xml :)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/transparent" />
<item android:state_pressed="true"
android:drawable="@drawable/transparent" />
<item android:state_selected="false"
android:drawable="@drawable/altercolor1"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
(RES /抽拉/ alterselector2.xml :)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/transparent" />
<item android:state_pressed="true"
android:drawable="@drawable/transparent" />
<item android:state_selected="false"
android:drawable="@drawable/altercolor2"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
(RES /值/ colors.xml :)
<resources>
<drawable name="transparent">#00ffffff</drawable>
<drawable name="altercolor1">#ffffffff</drawable>
<drawable name="altercolor2">#ff000000</drawable>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后使用setBackgroundResource方法在适配器的getView方法中设置背景:
if (position % 2 == 0){
reusableView.setBackgroundResource(R.drawable.alterselector1);
} else {
reusableView.setBackgroundResource(R.drawable.alterselector2);
}
Run Code Online (Sandbox Code Playgroud)
现在,当您选择一行时,您的背景不会隐藏原始选择器.
| 归档时间: |
|
| 查看次数: |
7837 次 |
| 最近记录: |