GridView VS GridLayout in Android Apps
Asked Answered
A

1

246

I have to use a Grid to implement Photo Browser in Android. So, I would like to know the difference between GridView and GridLayout.

So that I shall choose the right one.

Currently I'm using GridView to display the images dynamically.

Adeliaadelice answered 3/7, 2012 at 8:32 Comment(0)
E
295

A GridView is a ViewGroup that displays items in two-dimensional scrolling grid. The items in the grid come from the ListAdapter associated with this view.

This is what you'd want to use (keep using). Because a GridView gets its data from a ListAdapter, the only data loaded in memory will be the one displayed on screen. GridViews, much like ListViews reuse and recycle their views for better performance.

Whereas a GridLayout is a layout that places its children in a rectangular grid.

It was introduced in API level 14, and was recently backported in the Support Library. Its main purpose is to solve alignment and performance problems in other layouts. Check out this tutorial if you want to learn more about GridLayout.

Edelman answered 3/7, 2012 at 9:0 Comment(10)
GridLayout is also a ViewGroup.Teamster
I never said it wasn't. I was just emphasizing on the fact that it is a layout.Edelman
small addition: (good) adapters will load a little bit more than what is currently displayed onscreen so as to be able to provide that content as the user scrolls/moves to new, neighbouring content.Strohbehn
@laki one scenario would be if you want to load a bunch of images in a Grid like formation. Using GridView would only load in memory the views visible on screen. Whereas if you use a GridLayout you would have to load all the images that will be displayed at anytime causing an outofmemory error in most of devices.Wagner
@Wagner You wouldn't get an OutOfMemoryError if you use a ContentResolver to query the local data store, and scale your bitmaps with BitmapFactory and inSampleSize.Servia
Basic difference seems to be that GridView can accommodate only uniform column width throughout. Grid Layout allows you to have content spanning 2 or more columns. Grid Layout allows you to design screens having content spanning different column sizesBlotter
@laki Scenario one: Draw a calculator, GridLayout. Scenario two: Draw a gallery, GridView.Josephinajosephine
1. GridView is kind of equivalent to ListView / recycler view, where almost everything is dynamic and you would have an adapter attached to it. 2. Think of GridLayout as like scrollView where you would have to add child manually and give their position / margin / numOfColumns / rowsMistletoe
This answer is obsolete and there is a need for a current one.Hullo
@Hullo the question is not "what is the best way to display a scrolling grid on Android". It specifically asks the difference between GridView and GridLayout.Edelman

© 2022 - 2024 — McMap. All rights reserved.