Horizontal Scrolling Grid View

How to make grid view scroll horizontally not vertically in android?

You can try putting the GridView in a HorizontalScrollView. You may try to set fixed height to the GridView inside the HorizontalScrollView.

Then you can dynamically calculate the number of columns of the GridView based on your content and use setNumColumns(int) method to set it.

Horizontal scrolling grid view

You can

  • use a TableLayout inside a HorizontalScrollView, or
  • stay with your approach with an horizontal LinearLayout but adding vertical LinearLayouts instead of directly the images. E.g., adding three to four images per vertical LinearLayout in portrait, and redrawing to add only two in landscape.

I would try the TableLayout approach first.

PS1: for next time, try to remove all the non-relevant code (the less code is there, the easier is to understand what you did).

PS2: Remember that System.out is usually redirected to /dev/null and thus lost, so I strongly suggest you to use Log.d instead.

Complete example

Adapt this to the onCreate() method or wherever you need it:

public void horizontalScrollGalleryLayout () {
HorizontalScrollView sv = new HorizontalScrollView(this);
LinearLayout llh = new LinearLayout(this);
llh.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParamsTV = new LinearLayout.LayoutParams(40, 40);
LinearLayout.LayoutParams layoutParamsLL = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i=0; i<20; i++) {
LinearLayout llv = new LinearLayout(this);
llv.setOrientation(LinearLayout.VERTICAL);
TestView testView1 = new TestView(this, Color.rgb(i*12, 0, 0));
TestView testView2 = new TestView(this, true, Color.rgb(i*12, i*12, 0));
TestView testView3 = new TestView(this, true, Color.rgb(0, i*12, 0));
llv.addView(testView1, layoutParamsTV);
llv.addView(testView2, layoutParamsTV);
llv.addView(testView3, layoutParamsTV);
llh.addView(llv, layoutParamsLL);
}
sv.addView(llh, layoutParamsLL);
setContentView(sv);
}

I'm using a very simple View as an example:

public class TestView extends View {
Context context;
int color;

public TestView(Context context, int color) {
super(context);
this.context = context;
this.color = color;
}

@Override
public void onDraw (Canvas canvas) {
super.onDraw(canvas);
this.setBackgroundColor(Color.LTGRAY);
Paint paint = new Paint (Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
canvas.drawCircle(20, 20, 20, paint);
}
}

How to make grid-view horizontally scrollable in android

I found Two-way GridView helpful on github.

It has some methods:

scrollDirectionPortrait (vertical | horizontal)

scrollDirectionLandscape (vertical | horizontal)

numRows()

etc

horizontal scroll for css grid-layout

I found a satisfactory answer using CSS Grid layout.

In the grid layout, items are shrinking to fit within the layout.

I have defined the grid lines using fr in my question that is why the items are shrinking. Now I have used % because I want the cell items to flow out of visible area to produce scroll if needed.

and span keyword in defining grid cell areas helped a lot for auto placement of grid cells.

From MDN: span && [ <integer> || <custom-ident> ]

Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid item’s grid area is n lines from the opposite edge.

html,
body {
height: 100%;
margin: 0px;
}

.grid {
display: grid;
height: 100%;
background-color: lightgreen;
grid-template-columns: repeat(2, 50%);
grid-template-rows: repeat(2, 50%);
/*grid-gap: 5px;*/
grid-auto-columns: 50%;
grid-auto-flow: column;
}

.item {
border: 1px solid;
grid-area: span 1 / span 1;
}

.item:nth-child(odd) {
background-color: lightblue;
}

.item:nth-child(even) {
background-color: lightslategray;
}

.item1 {
grid-area: span 1/ span 2;
}
<div class="grid">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item1">4</div>
<div class="item item2">5</div>
<div class="item item3">6</div>
</div>

Flutter Gridview scrolling horizontal

Can you include the screenshot when the GridView rotate?

I think your code works fine with Axis.horizontal on. The problem might lie in the logic when calculating gridCrossAxisCount/ number of child items.

When Axis.horizontal is on, the mainAxis will now be horizontal and the crossAxis will be vertical.

Thus if the axisCount is 1, then all items will be display in 1 line only, but if it's 4, they will be display in 4 lines, which with 4 children in your example is not enough to cause overflow outside of Viewport and enable scrolling.

How to create horizontal scrolling grid view in android?

Answer 2 - also tested and working

Found a bit tricky answer but it works well, if carefully implemented.

It seems as simple as rotating the GridView by 90 degree and rotating the items inside GridView by -90 degree. But when doing this there are issues with layout sizing - explained below.

Even though GridView is rotated, the parent layout allocates space as per the dimensions of GridView without rotation. This will make GridView disappear.

WorkAround

Enclose GridView inside FrameLayout. Now set the dimension of FrameLayout according to the actual dimensions of horizontal GridView that you would like to see.

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp">

<GridView android:id="@+id/photoGrid"
android:listSelector="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:clickable="true"
android:singleLine="true"
android:text="@string/photos"
android:textColor="#000000"
android:textSize="16sp"
android:focusable="true" />

<TextView
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:fontFamily="sans-serif-medium"
android:text="@string/all_photos"
android:textColor="#2196F3"
android:textSize="14sp" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp">

<GridView android:id="@+id/clgGrid"
android:listSelector="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:clickable="true"
android:singleLine="true"
android:text="@string/collages"
android:textColor="#000000"
android:textSize="16sp"
android:focusable="true" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp">

<GridView android:id="@+id/cameraGrid"
android:listSelector="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:clickable="true"
android:singleLine="true"
android:text="@string/camera"
android:textColor="#000000"
android:textSize="16sp"
android:focusable="true" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp">

<GridView android:id="@+id/drawingGrid"
android:listSelector="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:clickable="true"
android:singleLine="true"
android:text="@string/drawing"
android:textColor="#000000"
android:textSize="16sp"
android:focusable="true" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp">

<GridView android:id="@+id/colorBgGrid"
android:listSelector="@android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:clickable="true"
android:singleLine="true"
android:text="@string/color_backgrounds"
android:textColor="#000000"
android:textSize="16sp"
android:focusable="true" />
</FrameLayout>
</LinearLayout>
</ScrollView>

Now in you Activity

int[] gridIds = new int[]{R.id.photoGrid, R.id.clgGrid, R.id.cameraGrid, R.id.drawingGrid, R.id.colorBgGrid};
GridView[] grids = new GridView[gridIds.length];

for(int i = 0; i<gridIds.length; i++) grids[i] = findViewById(gridIds[i]);
ViewGroup.LayoutParams params = grids[photo].getLayoutParams();
ViewGroup.MarginLayoutParams params1 = (ViewGroup.MarginLayoutParams) grids[photo].getLayoutParams();
for(int i = 0; i<gridIds.length; i++) {
grids[i].setRotation(-90);
params.height = size.x;
grids[i].setLayoutParams(params);
params1.topMargin = -size.x/2 + imgSize - 15*dp;
grids[i].setLayoutParams(params1);
grids[i].setAdapter(new cAdapter(cursor, 0));
}

Now Inside you adapter

    public class holder {
holder(View v){
img = v.findViewById(R.id.img);
txt = v.findViewById(R.id.txt);
}
ImageView img;
TextView txt;
}
public class cAdapter extends CursorAdapter {

cAdapter(Cursor c, int flags) {
super(mContext, c, flags);
}

@Override
public int getCount() {
int c = super.getCount();
return c < MAX_IMG ? c : MAX_IMG;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = LayoutInflater.from(mContext).inflate(R.layout.li_main, parent, false);
v.setTag(new holder(v));
return v;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
holder h = (holder) view.getTag();
final String imgPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
int rotation = 90;
try {
ExifInterface ei = new ExifInterface(imgPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation){
case ExifInterface.ORIENTATION_ROTATE_90:
rotation += 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotation += 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotation += 270;
break;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
h.img.setRotationX(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
h.img.setRotationY(180);
break;
}
} catch (IOException ignored) {}
h.img.setRotation(rotation);
Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(cr, cursor.getLong(0), MediaStore.Images.Thumbnails.MINI_KIND, null);
h.img.setImageBitmap(getRoundedCornerBitmap(b, 6*dp));
h.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, ImageEditActivity.class).putExtra("path", imgPath));
}
});
}
}

How can I add a horizontal scrolling gridview inside a vertical listview?

For getting the result shown in the image, the best approach would be to use a ListView() instead of using a GridView.

You can simply achieve the desired output by just adding one line of code in your normal ListView. The line that you need to add in your ListView is:

scrollDirection: Axis.horizontal

This will get your job done.

Single Row Horizontally Scrolling/Swipeable GridView

You have everything right but the Orientation for the ItemsWrapGrid must be Vertical in order to have an Horizontal ScrollViewer.

The documentation here https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.itemswrapgrid.aspx explains that:

When the value is Vertical, the grid adds items in columns from top to bottom, then wraps from left to right. Columns of items scroll or pan horizontally.



Related Topics



Leave a reply



Submit