Hosein Bitaraf Hosein Bitaraf - 5 months ago 49
Android Question

Using right to left direction in gridview

I want to use right to left direction in gridview. By default gridview shows contents left to right.

For example:

If I have this array and I want to specify 3 column for it:

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


GridView shows it like this:

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


but I want to show it like this:

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

Answer

By the suggestion of "Dmytro Danylyk" i use this function and solve my problem.

   /** 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;

        }
Comments