--------------------- Adapter ---------------------
package com.example.visorimagenes;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImagenAdapter extends BaseAdapter {
private Context mContext;
public ImagenAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4, R.drawable.image5};
}
---------------------Activity---------------------
package com.example.visorimagenes;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
@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(90);
// gridview.setNumColumns(GridView.AUTO_FIT);
gridview.setNumColumns(3);
gridview.setAdapter(new ImagenAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// 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.activity_main, menu);
return true;
}
}
XML MENUOPCIONES
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/menu_opcion1"
android:title="Opcion1"
android:icon="@drawable/image1"></item>
<item android:id="@+id/menu_opcion2"
android:title="Opcion 2"
android:icon="@drawable/image2"></item>
<item android:id="@+id/menu_opcion3"
android:title="Opcion 3"
android:icon="@drawable/image3"></item>
</menu>
Agregar metodos
@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 1",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_opcion3:
Toast.makeText(MainActivity.this, "Menu Opcion 1",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
//return super.onOptionsItemSelected(item);
}
package com.example.visorimagenes;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImagenAdapter extends BaseAdapter {
private Context mContext;
public ImagenAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4, R.drawable.image5};
}
---------------------Activity---------------------
package com.example.visorimagenes;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
@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(90);
// gridview.setNumColumns(GridView.AUTO_FIT);
gridview.setNumColumns(3);
gridview.setAdapter(new ImagenAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// 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.activity_main, menu);
return true;
}
}
XML MENUOPCIONES
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/menu_opcion1"
android:title="Opcion1"
android:icon="@drawable/image1"></item>
<item android:id="@+id/menu_opcion2"
android:title="Opcion 2"
android:icon="@drawable/image2"></item>
<item android:id="@+id/menu_opcion3"
android:title="Opcion 3"
android:icon="@drawable/image3"></item>
</menu>
Agregar metodos
@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 1",Toast.LENGTH_SHORT).show();
break;
case R.id.menu_opcion3:
Toast.makeText(MainActivity.this, "Menu Opcion 1",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
//return super.onOptionsItemSelected(item);
}
Comentarios
Publicar un comentario