Clase Adapter
package com.example.visorimagenes; import android.content.Context; import android.graphics.Color; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.GridView; import android.widget.ImageView; public class ImagenAdapter extends BaseAdapter { private Context mContext; private Integer [] mThumbIds = null; public ImagenAdapter(Context c, Integer [] imagenes) { mContext = c; mThumbIds = imagenes; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } /* * getView se llama automaticamente recuperando cada uno de los elementos * de mi getCount * (non-Javadoc) * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) */ @Override public View getView(int position, View convertView, ViewGroup parent) { Button buttonMenu; if (convertView == null) { // if it's not recycled, initialize some // attributes buttonMenu = new Button(mContext); buttonMenu.setText("Opcion " + (position + 1) ); GridView.LayoutParams params = new GridView.LayoutParams( GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT); buttonMenu.setLayoutParams(params); buttonMenu.setPadding(8, 8, 8, 8); buttonMenu.setBackgroundColor(Color.WHITE); } else { buttonMenu = (Button) convertView; } int imagen = mThumbIds[position]; buttonMenu.setCompoundDrawablesWithIntrinsicBounds(0, imagen, 0, 0); return buttonMenu; } }
Activitie
package com.example.visorimagenes; import com.example.visorimagenes.R.drawable; import android.os.Bundle; import android.app.Activity; import android.graphics.SweepGradient; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.menugrid); GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); //gridview.setColumnWidth(125); //gridview.setColumnWidth(GridView.STRETCH_SPACING_UNIFORM); // gridview.setNumColumns(GridView.AUTO_FIT); //gridview.setNumColumns(3); Integer [] imagenes = {R.drawable.miau, R.drawable.gato, R.drawable.gusano, R.drawable.lobo, R.drawable.charisar, R.drawable.pikachu}; ImagenAdapter adapter = new ImagenAdapter(this, imagenes); gridview.setAdapter(adapter); gridview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { Log.v("onItemClick", "entro al click"); // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menuopciones, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.menu_opcion1: Toast.makeText(MainActivity.this, "Menu Opcion 1",Toast.LENGTH_SHORT).show(); break; case R.id.menu_opcion2: Toast.makeText(MainActivity.this, "Menu Opcion 2",Toast.LENGTH_SHORT).show(); break; case R.id.menu_opcion3: Toast.makeText(MainActivity.this, "Menu Opcion 3",Toast.LENGTH_SHORT).show(); break; default: break; } return false; //return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { // TODO Auto-generated method stub Log.v("Onclick", ""); } }
Comentarios
Publicar un comentario