int []数字= {1,2,3,4,5};

我的ListView中有12行,

我希望第一个项目在数组中具有最低的索引,而最后一个项目在数组中具有最高的索引。

原因是因为第一个项目和最后一个项目之间会出现淡入淡出,并且无论我有多少个列表项目,我都希望这5种颜色都可以。

但是什么时候发生

如果项目= 12,

1有数字1

2有数字1

3有数字2

4有2

等等...

如果我有更多数字或更多物品怎么办?我完全傻吗?喊叫!

编辑:

第一项:0%,

最后一项:100%,

无论有多少物品!

最佳答案

我从您的评论中注意到了这一点:item.setColorFilter( Color.parseColor( allColors[ ( position / itemCount ) ] ), Mode.SRC_ATOP);

您正在使用position / itemCount。我假设这两个都是整数,这意味着由于position小于itemCount,因此该除法将始终返回0。您要做的是在项目列表中找到某个项目的相对位置。然后可以使用该位置在号码列表中找到相应的位置。

double pos = (double)position/(double)itemCount; // item 5 has 5/12 = 0.42
int colorPos = pos*allColors.length(); // Find position based on number of colors
// item 5: 0.42*5 = 2.1, cast to int ->2. 5 gets color at index 2

item.setColorFilter( Color.parseColor( allColors[ (colorPos) ] ), Mode.SRC_ATOP);


希望这可以帮助!

10-06 14:27
查看更多