【问题标题】:Using right to left direction in gridview在gridview中使用从右到左的方向
【发布时间】:2012-05-08 14:48:00
【问题描述】:

我想在 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]

【问题讨论】:

    标签: android gridview


    【解决方案1】:

    根据“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;
    
            }
    

    【讨论】:

      【解决方案2】:

      您不能只修改您的列表以符合您的要求吗?

      之前

      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");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-25
        • 2015-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-03
        • 1970-01-01
        相关资源
        最近更新 更多