GridView

Top  Previous  Next

Tela

 

xml

 
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:orientation="vertical" >

 

  <TextView

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="Exemplo de grid" />

 

  <GridView

      android:id="@+id/grid1"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:columnWidth="40dip"

      android:gravity="center"

      android:horizontalSpacing="10dip"

      android:numColumns="auto_fit"

      android:padding="10dip"

      android:verticalSpacing="10dip" />

 

</LinearLayout>
 

java

 
import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.GridView;

import android.widget.Toast;

 

public class LayoutsActivity extends Activity {

 

 private int[] imagens = { R.drawable.smile, R.drawable.smile,

                 R.drawable.smile, R.drawable.smile, R.drawable.smile,

                 R.drawable.smile, R.drawable.smile, R.drawable.smile };

 

 @Override

 public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.gridview);

 

         GridView grid = (GridView) findViewById(R.id.grid1);

         grid.setAdapter(new AdaptadorImagem(this, imagens,

                         new GridView.LayoutParams(30, 30)));

 

         grid.setOnItemClickListener(new OnItemClickListener() {

                 public void onItemClick(AdapterView parent, View v, int posicao,

                                 long id) {

                         Toast.makeText(LayoutsActivity.this, "Selecionada: " + posicao,

                                         Toast.LENGTH_SHORT).show();

                 }

         });

 }

}

 

AdaptadorImagem.java

 

package com.laioutes;

 

import android.content.Context;

import android.view.View;

import android.view.ViewGroup;

import android.view.ViewGroup.LayoutParams;

import android.widget.BaseAdapter;

import android.widget.ImageView;

 

public class AdaptadorImagem extends BaseAdapter {

 

 private Context ctx;

 private final int[] imagens;

 private final LayoutParams params;

 

 public AdaptadorImagem(Context c, int[] imagens, LayoutParams params)

 {

         this.ctx = c;

         this.imagens = imagens;

         this.params = params;

 }

 

 public int getCount() {

         return imagens.length;

 }

 

 public Object getItem(int posicao) {

         return posicao;

 }

 

 public long getItemId(int posicao) {

         return posicao;

 }

 

 public View getView(int posicao, View convertView, ViewGroup parent) {

         ImageView img = new ImageView(ctx);

         img.setImageResource(imagens[posicao]);

         img.setAdjustViewBounds(true);

         img.setLayoutParams(params);

         return img;

 }

 

}