2012-05-08 31 views
2

我想在gridview中使用從右到左的方向。默認情況下,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] 

回答

4

通過 「德米特羅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; 

     } 
0

無法修改您的清單以符合您的要求嗎?

之前

List<String> list = new ArrayList<String>(); 
list .add("Element 1"); 
list .add("Element 2"); 
list .add("Element 3"); 

List<String> list = new ArrayList<String>(); 
list .add("Element 3"); 
list .add("Element 2"); 
list .add("Element 1"); 
相關問題