一、GridView
1、简述
GridView按照行列来显示图片或文本的一种视图,排列其实有点类似TableLayout布局,
不过和TableLayout还是差别很大的,倒比较像二维的ListView。位于android.widget包下,常常用来实现类似九宫格的图像。
实现手法和ListView有点相似,用Apapter把数据绑定到控件上,为容器提供子视图,利用视图的数据和元数据来构建每个子视图自定义Adapter显示。
如果需要自定义适配器
可以扩展抽象类BaseAdapter。
2、常用属性和对应方法如下:
下面用实例化SimpleAdapter和继承抽象类BaseAdapter方法实现下图的例子:
3、实例化SimpleAdapter实现
Activity代码:
1 private GridView gv; 2 @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 // TODO Auto-generated method stub 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.gridview); 7 8 //准备要添加的数据条目 9 List<Map<String, Object>> items = new ArrayList<Map<String,Object>>(); 10 for (int i = 0; i < 9; i++) { 11 Map<String, Object> item = new HashMap<String, Object>(); 12 item.put("imageItem", R.drawable.ic_launcher);//添加图像资源的ID 13 item.put("textItem", "icon" + i);//按序号添加ItemText 14 items.add(item); 15 } 16 17 //实例化SimpleAdapter适配器 18 SimpleAdapter adapter = new SimpleAdapter(this, 19 items, 20 R.layout.grid_item, 21 new String[]{"imageItem", "textItem"}, 22 new int[]{R.id.image_item, R.id.text_item}); 23 24 25 //获得GridView实例 26 gv = (GridView)findViewById(R.id.mygridview); 27 //为GridView设置适配器 28 gv.setAdapter(adapter); 29 30 }
单个组件XML:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/RelativeLayout1" 4 android:layout_width="wrap_content" 5 android:layout_height="fill_parent" 6 android:paddingBottom="6dip" 7 > 8 <ImageView 9 android:id="@+id/image_item" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_centerHorizontal="true" 13 android:src="@drawable/ic_launcher" 14 /> 15 <TextView 16 android:id="@+id/text_item" 17 android:layout_below="@+id/image_item" 18 android:layout_height="wrap_content" 19 android:layout_width="wrap_content" 20 android:layout_centerHorizontal="true" 21 /> 22 </RelativeLayout>
页面主布局XML:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 > 6 <GridView 7 android:id="@+id/mygridview" 8 android:numColumns="3" 9 android:gravity="center_horizontal" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:stretchMode="columnWidth" 13 /> 14 </LinearLayout>
4、继承抽象类BaseAdapter实现
替换成自己GridAdapter代码:
1 // //准备要添加的数据条目 2 // List<Map<String, Object>> items = new ArrayList<Map<String,Object>>(); 3 // for (int i = 0; i < 9; i++) { 4 // Map<String, Object> item = new HashMap<String, Object>(); 5 // item.put("imageItem", R.drawable.ic_launcher);//添加图像资源的ID 6 // item.put("textItem", "icon" + i);//按序号添加ItemText 7 // items.add(item); 8 // } 9 // 10 // //实例化SimpleAdapter适配器 11 // SimpleAdapter adapter = new SimpleAdapter(this, 12 // items, 13 // R.layout.grid_item, 14 // new String[]{"imageItem", "textItem"}, 15 // new int[]{R.id.image_item, R.id.text_item}); 16 17 18 List<GridInfo> list = new ArrayList<GridInfo>(); 19 for (int i = 0; i < 9; i++) { 20 list.add(new GridInfo("icon"+i)); 21 } 22 23 GridAdapter adapter = new GridAdapter(this); 24 adapter.setList(list);
补上GridAdapter代码:
1 public class GridAdapter extends BaseAdapter { 2 3 private class GridHolder { 4 ImageView appImage; 5 TextView appName; 6 } 7 8 private Context context; 9 10 private List<GridInfo> list; 11 private LayoutInflater mInflater; 12 13 public GridAdapter(Context c) { 14 super(); 15 this.context = c; 16 } 17 18 public void setList(List<GridInfo> list) { 19 this.list = list; 20 mInflater = (LayoutInflater) context 21 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 22 23 } 24 25 public int getCount() { 26 // TODO Auto-generated method stub 27 return list.size(); 28 } 29 30 @Override 31 public Object getItem(int index) { 32 33 return list.get(index); 34 } 35 36 @Override 37 public long getItemId(int index) { 38 return index; 39 } 40 41 @Override 42 public View getView(int index, View convertView, ViewGroup parent) { 43 GridHolder holder; 44 if (convertView == null) { 45 convertView = mInflater.inflate(R.layout.grid_item, null); 46 holder = new GridHolder(); 47 holder.appImage = (ImageView)convertView.findViewById(R.id.image_item); 48 holder.appName = (TextView)convertView.findViewById(R.id.text_item); 49 convertView.setTag(holder); 50 51 }else{ 52 holder = (GridHolder) convertView.getTag(); 53 54 } 55 GridInfo info = list.get(index); 56 if (info != null) { 57 holder.appName.setText(info.getName()); 58 } 59 return convertView; 60 } 61 62 }
比较麻烦还是重写getView()方法。里面的GridInfo只是普通的JAVA bean类。