jzafrilla
jzafrilla

Reputation: 1436

Android GridView load items vertically

Is it possible to load items in GridView with vertical orientation? I mean...

Normal situation - 3 columns

enter image description here

-----------------------------             
1      |  2      |    3
-----------------------------
4      |  5      |    6

enter image description here

Desired situation - 3 columns

-----------------------------              
1      |  3      |    5
-----------------------------
2      |  4      |    6

Thanks in advance!

Upvotes: 21

Views: 9101

Answers (8)

vinay reddy
vinay reddy

Reputation: 9

I Know the Question is bit old.. but i didn't find any Relevant answer so i Created one algorithm.

in Below code crossAxisCount is Nothing but a How many Grids aligned Horizontally i.e., in the above picture in portrait view we have '2' grids and in landscape we have '4' grids. arrayLength means Total number of grids we have to provide to get that layout.

  final int crossAxisCount = 2;
  final int arrayLength = 8;
  final List<int> output = [];
  
  
  
  var runiCheck = 0;
  for (var i = 0; i < (arrayLength/ crossAxisCount).ceil(); i++) {
    var runjCheck = -1;
    for(var j = i; j <= arrayLength; j+= (arrayLength / crossAxisCount).ceil()){
      runiCheck++;
       if((arrayLength % crossAxisCount != 0)){
         if(arrayLength < runiCheck) break;
           runjCheck++; 
        if(arrayLength % crossAxisCount < runjCheck){
          j--;         
        }            
      }else{
         runjCheck++;   
         if(runjCheck >= crossAxisCount) break;        
       }     
      output.add(j);    
    }    

  }
  print(output);

output:

[
0, 4,
1, 5, 
2, 6, 
3, 7
]

if we Change crossAxisCount to 4 and arrayLength to 21(just a random numbers.)


  final int crossAxisCount = 4;
  final int arrayLength = 21;
  final List<int> output = [];
  
  

The output will be:

[
0, 6, 11, 16, 
1, 7, 12, 17, 
2, 8, 13, 18, 
3, 9, 14, 19, 
4, 10, 15, 20, 
5
]

I Know Using This Algorithm in very long lists/ arrays are bit expensive. but this is the Exact Solution.

I am new to Stack overflow please up vote my answer to get Motivated.

Upvotes: 0

Abhilasha
Abhilasha

Reputation: 51

If you are using GridLayoutManager with RecyclerView, easy way to fill vertically data first is :

GridLayoutManager lLayout_artist = new GridLayoutManager(getActivity(),3,GridLayoutManager.HORIZONTAL, false);

You can change orientation of GridLayoutManager as per your requirement.

Upvotes: 1

Ahmad Muzakki
Ahmad Muzakki

Reputation: 1088

I think you need https://github.com/ApmeM/android-flowlayout . and set child gravity to Gravity.FILL_HORIZONTAL

they even have vertical or horizontal orientation

Upvotes: 0

Hiren Patel
Hiren Patel

Reputation: 52790

// Have you ever used GridLayout this, Try this one:

File -> Import -> <sdk_folder>\extras\android\support\v7\gridlayout>

// Add this project as library project

// xml file - activity_main.xml

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/gridLayout"
    tools:context="com.example.gridlayoutdemo.MainActivity" >

</android.support.v7.widget.GridLayout>

// Java code

MainActivity.class

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.GridLayout;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    private GridLayout gridLayout;
    private TextView[] textViews;
    private int noOfItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridLayout = (GridLayout) findViewById(R.id.gridLayout);
        gridLayout.setOrientation(1);
        gridLayout.setColumnCount(3);
        gridLayout.setRowCount(3);

        textViews = new TextView[8];

        for (int i = 0; i < 8; i++) {
            textViews[i] = new TextView(MainActivity.this);
            textViews[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            textViews[i].setText(String.valueOf(i));
            textViews[i].setTextSize(25);
            textViews[i].setPadding(50, 25, 10, 25);
            gridLayout.addView(textViews[i]);
        }

        setContentView(gridLayout);

        for (noOfItems = 0; noOfItems < 8; noOfItems++) {
            textViews[noOfItems].setOnClickListener(new View.OnClickListener() {

                int pos = noOfItems;

                public void onClick(View v) {
                    Toast.makeText(getBaseContext(), pos + " Clicked",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

}

// If you want to display horizontally than do this gridLayout.setOrientation(0);

Upvotes: 0

Ashwin N Bhanushali
Ashwin N Bhanushali

Reputation: 3882

I don't think this is possible at all.

Reason ::

  • There is technical glitch doing this since we want to scroll items vertically. And also the rendering items on the screen is also vertical direction. In this case we can not complete the calculation we need to do in the onLayout and onMeasure Method of the GridView.
  • If try another way i.e some of us has suggested that snuffle the datasource as [1,5,2,6,3,7,4,8, ...] but in this case what would be shuffle algorithm?
  • When user flings vertically then how can you calculate how many items we need to render vertically and when to switch to next column to render the other items. there is no such mathematical formula for it.

I did some work on this and came to this conclusion.Hope this make unanswered question answered.

Upvotes: 3

Subramanian Ramsundaram
Subramanian Ramsundaram

Reputation: 1347

You can Achieve this by using GridLayout instead of GridView.

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:rowCount="2">
</GridLayout>

Upvotes: -1

You-Liang Shih
You-Liang Shih

Reputation: 61

Is it possible to change the sequence of data? In your example, change data to [1,3,5,2,4,6, ...] for horizontal orientation, [1,5,2,6,3,7,4,8, ...] for vertical orientation.

Upvotes: 0

Soham
Soham

Reputation: 4960

Please clarify, say you have a Grid like this

1  4  7
2  5  8
3  6  9

now if you scroll it down vertically, it should basically be like this??

2  5  8
3  6  9
4  7  10

In this case,

  • items 4 and 7 are not recycled, in fact they are moved to a new position
  • item 1 is to be recycled
  • only item 10 is introduced

Clearly, this is not how a normal ListView/GridView works where a row is recycled at a time, AFAIK you will need to customise GridView to get this functionality.

Upvotes: 0

Related Questions