Reputation: 1436
Is it possible to load items in GridView with vertical orientation? I mean...
Normal situation - 3 columns
-----------------------------
1 | 2 | 3
-----------------------------
4 | 5 | 6
Desired situation - 3 columns
-----------------------------
1 | 3 | 5
-----------------------------
2 | 4 | 6
Thanks in advance!
Upvotes: 21
Views: 9101
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
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
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
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
Reputation: 3882
I don't think this is possible at all.
Reason ::
I did some work on this and came to this conclusion.Hope this make unanswered question answered.
Upvotes: 3
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
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
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,
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