GridView实践

     Grid为网格视图,例如实现类似九宫格的效果,可以首选GridView,该组件中的每一个条目通过ListAdapter和该组件进行关联。

GridViewExample.java代码如下:

 1 package com.example.lenovo.a1106;
 2 import android.app.Activity;
 3 import android.content.Intent;
 4 import android.content.pm.ResolveInfo;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.widget.BaseAdapter;
 9 import android.widget.GridView;
10 import android.widget.ImageView;
11 
12 
13 
14 import java.util.List;
15 
16 
17 public class GridViewExample extends Activity {
18     private GridView myGridView;
19     private List<ResolveInfo> myAppIcon;     //声明变量,存放桌面应用程序图标
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.gridview);
25         myGridView = (GridView) findViewById(R.id.myGrid);
26         loadAppIcon(); //加载桌面图片
27         BaseAdapter adapter = new BaseAdapter() {
28             @Override
29             public int getCount() {    //项目个数
30                 return myAppIcon.size();
31             }
32 
33             @Override
34             public Object getItem(int position) {  //获取指定位置的项目
35                 return myAppIcon.get(position);
36             }
37 
38             @Override
39             public long getItemId(int position) {   //获取指定位置项目id
40                 return position;
41             }
42 
43             //定义每一项显示的内容
44             @Override
45             public View getView(int position, View convertView, ViewGroup parent) {
46                 ImageView imageView;
47                 if (convertView == null) {
48                     imageView = new ImageView(GridViewExample.this);
49                     //创建Image对象
50 
51                     imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
52                     //设置图片的填充方式,这里按比例拉伸图片
53                     imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
54                     //设置ImageView的大小为50*50
55                 } else {
56 
57                     imageView = (ImageView) convertView;
58                 }
59                 ResolveInfo info = myAppIcon.get(position);
60                 //获取myAppIcon中下标为position的ResolveInfo
61                 imageView.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
62                 return imageView;
63 
64             }
65         };
66        myGridView.setAdapter(adapter);
67 
68       //  myListView.setAdapter(adapter);
69     }
70 
71     private void loadAppIcon() {
72 
73         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
74         //创建Intent
75         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
76         //添加桌面应用程序列表到Intent
77         myAppIcon = getPackageManager().queryIntentActivities(mainIntent, 0);
78     }
79 }
View Code

gridview.xml代码如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <GridView
 4     xmlns:android="http://schemas.android.com/apk/res/android"
 5     android:id="@+id/myGrid"
 6     android:orientation="vertical"
 7     android:layout_width="match_parent"
 8     android:layout_height="match_parent"
 9     android:padding="10dp"
10     android:numColumns="auto_fit"
11     android:horizontalSpacing="10dp"
12     android:verticalSpacing="10dp"
13     android:stretchMode="columnWidth"
14     android:gravity="center" />

 

 

当时遇到了个错误

说是空对象,然而没有想到是没有加载布局文件!!!得长记性!!!

 

posted @ 2015-11-06 23:15  白一  阅读(311)  评论(0编辑  收藏  举报