组后台后的ListView犯规显示颜色的onclick [英] ListView doesnt show onclick color after set background
问题描述
我有有一个适配器,它工作正常列表视图,但后来我加了一些code,因为我想行是不同的颜色,在这之后,的onclick颜色犯规露面。
下面是code我说:
如果(在位置%2 == 0)
vi.findViewById(R.id.parentL).setBackgroundColor(ctx.getResources()的getColor(R.color.row1));
其他
vi.findViewById(R.id.parentL).setBackgroundColor(ctx.getResources()的getColor(R.color.row2));
TL:DR
- 的列表视图项目使用选择
- 您需要设置你的整体的ListView的背景是透明。
说明
首先,你需要设置你的ListView为是透明的。
在您的适配器的onCreate()
方法设置:
((ListView控件)findViewById(R.id.main_list))setBackgroundColor(0);
接下来,在适配器的
getView()
包括以下code
如果(在位置%2 == 0){
view.setBackgroundResource(R.drawable.selector);
}其他{
view.setBackgroundResource(R.drawable.selector2);
}
其中selector.xml为
<?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目的android:STATE_ pressed =false的机器人:state_focused =假
机器人:可绘制=@色/蓝色/>
<项目的android:state_enabled =false的机器人:state_focused =真
机器人:可绘制=@色/ translucent_red/>
<项目的android:state_enabled =真
机器人:STATE_ pressed =真正的机器人:可绘制=@色/ translucent_red/>
<项目的android:state_enabled =真
机器人:state_focused =真正的机器人:可绘制=@色/ translucent_red/>
<项目
机器人:可绘制=@颜色/安卓透明/>
< /选择>
和selector2.xml是
<?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目的android:STATE_ pressed =false的机器人:state_focused =假
机器人:可绘制=@色/绿色/>
<项目的android:state_enabled =false的机器人:state_focused =真
机器人:可绘制=@色/ opaque_red/>
<项目的android:state_enabled =真
机器人:STATE_ pressed =真正的机器人:可绘制=@色/ opaque_red/>
<项目的android:state_enabled =真
机器人:state_focused =真正的机器人:可绘制=@色/ opaque_red/>
<项目
机器人:可绘制=@颜色/安卓透明/>
< /选择>
不要忘了包括colors.xml
<?XML版本=1.0编码=UTF-8&GT?;
<资源>
<颜色名称=opaque_red>#F00< /彩色>
<颜色名称=translucent_red>#80ff0000< /彩色>
<颜色名称=蓝色>#00F< /彩色>
<颜色名称=绿色>#0f0< /彩色>
< /资源>
示例项目
下面是我为您配置一个示例项目文件一>。你可能想任何事情之前做一个项目>清除。
某些文件可能会在此应用程序被忽略,因为我修改了做了一件非常不同的一个现有的样本。
截图
这里是我的示例应用程序看起来像
i have a listview which has an adapter, which worked properly, but then i added some code because i wanted the rows to be different colors, after that, the onclick color doesnt show up.
Here is the code i added:
if (position % 2 == 0)
vi.findViewById(R.id.parentL).setBackgroundColor(ctx.getResources().getColor(R.color.row1));
else
vi.findViewById(R.id.parentL).setBackgroundColor(ctx.getResources().getColor(R.color.row2));
TL:DR
- Use selectors for listview items
- You need to set your overall listview background to be to transparent.
Instructions
First, you need to set your ListView to to be transparent
Inside your Adapter's onCreate()
method, set:
((ListView) findViewById(R.id.main_list)).setBackgroundColor(0);
Next, inside Adapter
's getView()
include the following code
if (position%2 == 0) {
view.setBackgroundResource(R.drawable.selector);
} else {
view.setBackgroundResource(R.drawable.selector2);
}
where selector.xml is
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@color/blue" />
<item android:state_enabled="false" android:state_focused="true"
android:drawable="@color/translucent_red" />
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="@color/translucent_red" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="@color/translucent_red" />
<item
android:drawable="@color/android:transparent" />
</selector>
and selector2.xml is
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@color/green" />
<item android:state_enabled="false" android:state_focused="true"
android:drawable="@color/opaque_red" />
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="@color/opaque_red" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="@color/opaque_red" />
<item
android:drawable="@color/android:transparent" />
</selector>
don't forget to include a colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
<color name="blue">#00f</color>
<color name="green">#0f0</color>
</resources>
Sample Project
here are the files for a sample project I configured for you. You probably want to do a Project > Clean before anything else.
Some files can be ignored in this app, since I modified an existing sample that did something very different.
Screenshot
here is what my example app looked like
这篇关于组后台后的ListView犯规显示颜色的onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!