4

I'm using a GridView to show a list of TV channels using a SimpleCursorAdapter to retrieve channel name from a db. Default behaviour is to fill the grid left -> right, top -> bottom as in...

1 2
3 4
5 6

The GridView is fixed with 2 columns and obviously is scrollable vertically if it exceeds the vertical bounds of its parent.

I have a user who would prefer each column to be filled top->bottom before moving on to the next so effectively I'm looking at having a fixed number of rows with a 'grid' of some sort scrollable horizontally, example with 9 rows (fixed), no vertical scrolling, 2 columns visible and horizontal scrolling...

1 10 | 19
2 11 | 20
3 12 | 21
4 13 | 22
5 14 | 23  <-> 19-27 hidden, but columns scrollable horizontally
6 15 | 24
7 16 | 25
8 17 | 26
9 18 | 27

I understand GridView itself cannot do this but is there an existing view that would behave like this? If not, what would be the best approach to creating a custom view?

Thanks.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • possible duplicate of [GridView with horizontal scroll](http://stackoverflow.com/questions/2997818/gridview-with-horizontal-scroll) – Matthew Mar 18 '11 at 15:40
  • @Matthew: OK, admitted my title mentions GridView specifically but I'd already figured I couldn't rotate/orientate GridView's layout. My question is whether there's a view which does this or how best to approach this scenario. I'll edit my title, thanks for pointing that out. – Squonk Mar 18 '11 at 16:32
  • @Squonk Any luck with this mate? I have a similar question please see http://stackoverflow.com/questions/11577153/change-the-behaviour-of-a-gridview-to-make-it-scroll-horizontally-rather-than-ve – Arif Nadeem Jul 20 '12 at 11:11

1 Answers1

3

You can't do it with GridView. You would have to create a custom view to do this.

Edit - if you know how big your grid items are, you can cut some corners. GridView is complicated mostly because it deals with items of any size and loads them dynamically. An easier way for you might be:

  1. Create a HorizontalScrollView with a horizontal LinearLayout inside.
  2. Determining how many rows of your item will fit on the screen. Call this rows.
  3. while you still have items you need to layout:
    1. Create a vertical LinearLayout, adding rows or less items to it.
    2. Add your new vertical LinearLayout to the horizontal one.

There are some downsides versus what a "horizontal GridView" would get you:

  • All the views are loaded up immediately, which is bad for huge lists of items.
  • You need to know how big your items are, and they need to be the same size.

Upsides:

  • It's very easy to implement.
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • Thanks, I'd got as far as looking at HorizontalScrollView since posting my question but hadn't decided where to go next. My ideal would have been to have an adapterview <-> adapter relationship handle everything for me but I guess I'm going to have to handle everything myself. You've given me enough of a start though - could be fun! – Squonk Mar 18 '11 at 17:49
  • Yeah, you could implement your own AdapterView, which might be nice reusable way to do it. Looking at GridView.java in the Android source made me think that might be more trouble than it's worth. – Matthew Mar 18 '11 at 18:44
  • I've made some progress on this although it isn't a high priority so it's on a back-burner for now. I think I can make it work from your suggestions though. Thanks. – Squonk Mar 21 '11 at 10:43