我的列表中的某些项目有不同的颜色
我有一个包含项目的列表,有些项目可以处于“已查看”状态。我希望这些项目具有与其余项目不同的背景颜色。
列表视图基本上对整个列表有一个选择器,在其中一个项目上设置背景颜色不会起到作用,因为选择器是在我的项目布局下方绘制作为背景并依赖它们是透明的。
有没有一种方法可以为列表定义多个选择器?如果没有,是否可以创建一个具有额外状态的选择器?基本上我想要的是常规列表选择器加上一个额外的状态,它的 ColorDrawable 在我的 color.xml 中定义。(因为我无法从可绘制对象继承,并且 android 的 list_selector 可绘制对象在 SDK 中不可见供我使用我只想添加一个状态,但是如何强制使用额外的状态?)
I have a list with items, some items can be in 'viewed' status. i want those items to have different background color then the rest.
list view basically has a single selector for the whole list, setting a background color on one of the items prevent wont do the trick since the selector is drawn below my item's layout as background and counts on them being transparent.
is there a way to define more then one selector for a list ? if not is it possible to create a selector that has an extra state ? basically all i want is the regular list selector plus an extra state that it's ColorDrawable is defined in my colors.xml.(since i can't inherit from a drawable and the list_selector drawables of android are not visible in the SDK for me to use i wanted just to add a state, but then how do i enforce using the extra state ?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到了答案。
我的问题不是选择器的状态,我希望所有项目的选择器都处于相同的状态!
问题是,如果适配器中的项目中的某些布尔值为假,我希望该项目是常规的,如果布尔值为真,则颜色为 X,但即使颜色为 X,我仍然希望它在选择或按下后是透明的。
所以解决方案是下面的xml:
我发现即使列表本身是获取状态的列表,它也会将状态传递给它的子项(即列表项),而我认为它没有这样做。
只有 android:focused 在这里不起作用,android 列表项没有聚焦而是被选中...因此,正如您在任何未按下或选择的状态下看到的那样,我给出了默认颜色,否则它是透明的,并且选择器对所有人都可见。
在我的适配器中,我需要以下代码(在 getView() 方法中):
这确保每个项目都会获得正确的背景。当然,如果项目状态更改为已查看并且我希望它显示我需要在适配器上调用notifyDatasetCahnged()。
Found the answer.
My problem is not the state of the selector, i want the selector in the same state for ALL items!
the problem is that i want the item to be regular if certain boolean in items in the adapter is false and color X if the boolean is true, but even if the color is X i still want it to be transparent once it's selected or pressed.
So the solution is the following xml:
i found out the even though the list itself is the one that gets the state , it DOES pass the state on to it's children (I.E the list items) which i thought it didn't do.
Only android:focused wont be effective here, android list items are not focused but selected... so as you can see in any state that is not pressed or selected i give my default color, otherwise it's transparent and the selector is visible to all.
in my adapter i need the following code(in getView() method):
this makes sure every item will get the right background. of course if an item state is changed to viewed and i want it to show i need to call notifyDatasetCahnged() on the adapter.
您可以实现
Adapter
的自定义子类并重写方法getView()
。由于列表调用此方法来获取单个项目的View
,因此您可以通过重写getView()
来定义单个列表项目的外观。因此,更改已查看项目的背景颜色应该不成问题。更新
AbsListView
ListView 类扩展的 a> 类有一个名为setDrawSelectorOnTop()
。我认为你可以用它来控制选择器的行为。You can implement a custom subclass of your
Adapter
and override the methodgetView()
. As this method is called by the list to get theView
for a single item you can define how the a single list item looks like by overridinggetView()
. So changing the background color of an already viewed item shouldn't be a problem.UPDATE
The
AbsListView
class which theListView
class extends has a method calledsetDrawSelectorOnTop()
. I think you can use it to control the behavior of the selector.