Code example for GridView

Methods: setAdaptersetGravitysetNumColumnssetVerticalSpacing, setClipChildren, setOnItemClickListener, setPadding, setScrollBarStyle, setScrollingCacheEnabled, setSelector

0
		getData(albumList);
	} 
 
	// To set albums in gridview 
	public void getData(final List<Album> albumList) {
		final GridView view = new GridView(this);
		getGridProperties(view);
 
		view.setAdapter(new AlbumsAdapter(AlbumActivity.this, 0, albumList));
 
		String[] urls = new String[albumList.size()];
		for (int i = 0; i < albumList.size(); i++) {
			urls[i] = albumList.get(i).getCoverPhoto();
		} 
 
		view.setOnItemClickListener(new OnItemClickListener() {
			@Override 
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				showPhoto(view, albumList, position);
				photoListFlag = true;
			} 
		}); 
		dataSectionView.addView(view);
	} 
 
	/** 
	 * To load photos in Gridview 
	 *  
	 * @param gridview 
	 *            gridview 
	 * @param albumList 
	 *            albums list of provider 
	 * @param position 
	 *            Clicking position of provider 
	 */ 
 
	public void showPhoto(final GridView gridView, final List<Album> albumList, int position) {
 
		Album bean = albumList.get(position);
		Log.d("LifeView", "Album Clicked");
 
		final List<Photo> photoList = bean.getPhotos();
		PhotoAdapter photoAdapter = new PhotoAdapter(AlbumActivity.this, 0, photoList);
		gridView.setAdapter(photoAdapter);
		textView.setText(bean.getName());
 
		final ImageLoader imageLoader = new ImageLoader(AlbumActivity.this);
 
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override 
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
 
				Toast.makeText(AlbumActivity.this, "Loading Photo..... Please Wait", Toast.LENGTH_SHORT).show();
				Log.d("LifeView", "Photo Clicked");
				dataSectionView.setVisibility(View.GONE);
				photoListFlag = false;
				photoFlag = true;
				Photo photoBean = photoList.get(position);
 
				ImageView picture = (ImageView) AlbumActivity.this.findViewById(R.id.picture);
				TextView pictureTitle = (TextView) AlbumActivity.this.findViewById(R.id.pictureTitle);
				frameView.setVisibility(View.VISIBLE);
				imageLoader.DisplayImage(photoBean.getLargeImage(), picture);
 
				pictureTitle.setText(photoBean.getTitle());
 
			} 
		}); 
	} 
 
	/** 
	 * collection of grid properties 
	 */ 
	@SuppressWarnings("static-access") 
	public void getGridProperties(GridView view) {
		view.setNumColumns(3);
		view.setVerticalSpacing(5);
		view.setScrollBarStyle(view.SCROLLBARS_OUTSIDE_OVERLAY);
		view.setScrollingCacheEnabled(false);
		view.setGravity(Gravity.TOP);
		view.setSelector(new ColorDrawable(Color.parseColor("#00000000")));
		view.setClipChildren(true);
		view.setPadding(5, 5, 5, 5);
	} 
 
	public void clearView() { 
		dataSectionView.removeAllViews();
		dataSectionView.invalidate();