GridView 的内容可以使用SimpleAdapter设置,也可以通过自定义的BaseAdapter子类完成设置 (ImageAdapter)
下面是用SimpleAdapter实现的网格 适配器可用来填充各种组件
主程序 MyDemo.java
package com.jackie.gridviewproject;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
public class MyDemo extends Activity {
private GridView myGridView = null;
private SimpleAdapter simpleAdapter = null;
private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.initAdapter();
this.myGridView = (GridView) super.findViewById(R.id.myGridView);
this.myGridView.setAdapter(simpleAdapter);
this.myGridView.setOnItemClickListener(new OnItemClickListenerImpl());
}
private class OnItemClickListenerImpl implements OnItemClickListener {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ImageView imageView = new ImageView(MyDemo.this);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Map<String,Integer> map=(Map<String,Integer>) parent.getAdapter().getItem(position);
imageView.setImageResource(map.get("img"));
Dialog dialog=new AlertDialog.Builder(MyDemo.this)
.setIcon(R.drawable.file)
.setTitle("查看图片")
.setView(imageView).setNegativeButton("关闭", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
}).create();
dialog.show();
}
}
private void initAdapter() {
Field[] fields = R.drawable.class.getDeclaredFields(); // 利用反射机制取得图片资源
for (int x = 0; x < fields.length; x++) {
if (fields[x].getName().startsWith("pic_")) {
Map<String, Integer> map = new HashMap<String, Integer>();
try {
map.put("img", fields[x].getInt(R.drawable.class));
} catch (Exception e) {
}
this.list.add(map);
}
}
this.simpleAdapter = new SimpleAdapter(this, this.list,
R.layout.grid_layout, new String[] { "img" },
new int[] { R.id.img });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_demo, menu);
return true;
}
}
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
public class MyDemo extends Activity {
private GridView myGridView = null;
private SimpleAdapter simpleAdapter = null;
private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.initAdapter();
this.myGridView = (GridView) super.findViewById(R.id.myGridView);
this.myGridView.setAdapter(simpleAdapter);
this.myGridView.setOnItemClickListener(new OnItemClickListenerImpl());
}
private class OnItemClickListenerImpl implements OnItemClickListener {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ImageView imageView = new ImageView(MyDemo.this);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Map<String,Integer> map=(Map<String,Integer>) parent.getAdapter().getItem(position);
imageView.setImageResource(map.get("img"));
Dialog dialog=new AlertDialog.Builder(MyDemo.this)
.setIcon(R.drawable.file)
.setTitle("查看图片")
.setView(imageView).setNegativeButton("关闭", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
}).create();
dialog.show();
}
}
private void initAdapter() {
Field[] fields = R.drawable.class.getDeclaredFields(); // 利用反射机制取得图片资源
for (int x = 0; x < fields.length; x++) {
if (fields[x].getName().startsWith("pic_")) {
Map<String, Integer> map = new HashMap<String, Integer>();
try {
map.put("img", fields[x].getInt(R.drawable.class));
} catch (Exception e) {
}
this.list.add(map);
}
}
this.simpleAdapter = new SimpleAdapter(this, this.list,
R.layout.grid_layout, new String[] { "img" },
new int[] { R.id.img });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_demo, menu);
return true;
}
}
两个布局文件
主布局文件 main.xml 和网格布局文件grid_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyDemo" >
<GridView
android:id="@+id/myGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4"
android:stretchMode="columnWidth"/>
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyDemo" >
<GridView
android:id="@+id/myGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4"
android:stretchMode="columnWidth"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MyDemo" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:padding="3dp"/>
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MyDemo" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:padding="3dp"/>
</LinearLayout>