Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Grid View Scroll Horizontally

I am doing a task which retrieves images from server and displays in GridView in the application. This Grid view is scrolling up and down. But i want to scroll this view left to right as the menu screen scrolls. Is it possible with grid view? Or Is there any better way to do this? please help me in doing this.

thanks in advance.

like image 796
wolverine Avatar asked Sep 25 '12 05:09

wolverine


People also ask

How do I scroll horizontally in GridView?

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. Great idea!

Is GridView scrollable Android?

Grid view requires an adapter to fetch data from the resources. This view can be scrolled both horizontally and vertically. The scrolling ability of the GridView by default is set to enabled.


2 Answers

This isn't easily possible with the stock Android GridView. Try using this library: two-way-gridview

(I found this library in this other answer: Horizontal scrolling grid view)

like image 156
Anton I. Sipos Avatar answered Oct 20 '22 19:10

Anton I. Sipos


Is there any better way to do this?

Yes, there is.

You can achieve this in 3 lines using a RecyclerView with a horizontal GridLayoutManager:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rec1);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false));
recyclerView.setAdapter(new CustomAdapter(arrayList));

The RecyclerView supports applications built with the SDK 7 or larger.

If you want to make it even easier, take a look at the HorizontalGridView class if you are working with an application that is built for the API 17 or larger.


Here's a link of an example of a simple RecyclerView Adapter.

like image 45
Evin1_ Avatar answered Oct 20 '22 20:10

Evin1_