Possible to fill a 'grid-like' view by column rather than by row?
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 col开发者_JAVA百科umns 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.
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:
- Create a
HorizontalScrollView
with a horizontalLinearLayout
inside. - Determining how many rows of your item will fit on the screen. Call this
rows
. while
you still have items you need to layout:- Create a vertical
LinearLayout
, addingrows
or less items to it. - Add your new vertical
LinearLayout
to the horizontal one.
- Create a vertical
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.
精彩评论