gpt4 book ai didi

android - 在 gridview 中使用从右到左的方向

转载 作者:搜寻专家 更新时间:2023-11-01 08:10:12 24 4
gpt4 key购买 nike

我想在 gridview 中使用从右到左的方向。默认情况下,gridview 从左到右显示内容。

例如:

如果我有这个数组并且我想为它指定 3 列:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

GridView 显示如下:

[1 2 3]
[4 5 6]
[7 8 9]

但我想这样显示:

[3 2 1]
[6 5 4]
[9 8 7]

最佳答案

根据“Dmytro Danylyk”的建议,我使用了这个函数并解决了我的问题。

   /** Returns inverted list by step that take. for example if our list is {1, 2, 3, 4, 5, 6,
* 7 ,8 ,9} and step is 3 inverted list is this: {3, 2, 1, 6, 5, 4, 9, 8, 7}
*/
public static <E> ArrayList<E> invert(List<E> source, int step){
List<E> inverted = new ArrayList<E>();
for(int i = 0; i < source.size(); i++){
if((i + 1) % step == 0){
for(int j = i, count = 0; count < step; j--, count++){
inverted.add(source.get(j));
}
}
}

//
// When (source.size() % step) is not 0 acts.this is for last of list. add last part
// of the source that wasn't add.
//
int remainder = source.size() % step;
if((remainder) != 0 ){
for (int j = source.size() - 1, count = 0; count < (remainder); j--, count++) {
inverted.add(source.get(j));
}
}

return (ArrayList<E>) inverted;

}

关于android - 在 gridview 中使用从右到左的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10500967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com