Android 九宫格的实现

Android 九宫格的实现

                                                                                              2013-05-09         20:04:25     

       今天在浏览网页的时候看到了一篇有关九宫格实现的博文,感觉挺有意思。所以自己模仿做了一个,先贴出代码如下:

1、xml代码:

 1 <?xml version="1.0" encoding="utf-8"?> 
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 3      android:orientation="vertical"
 4      android:layout_width="fill_parent" 
 5      android:layout_height="fill_parent" 
 6      android:layout_weight="1.0" 
 7      android:background="@drawable/yellow" 
 8      > 
 9 <ImageView android:id="@+id/ImageView01" 
10            android:layout_width="100sp" 
11            android:layout_height="100sp"
12            android:layout_gravity="center_vertical"
13            android:background="@drawable/a"></ImageView>   
14 <GridView 
15     android:id="@+id/gridview"  
16     android:layout_width="wrap_content"    
17     android:layout_height="wrap_content"  
18     android:numColumns="3"  
19     android:verticalSpacing="30dip"  
20     android:horizontalSpacing="10dip"  
21     android:columnWidth="90dip" 
22     android:stretchMode="columnWidth" 
23     android:gravity="center"
24     android:listSelector="@drawable/c"
25     >
26 </GridView>
27 </LinearLayout>

   其中android:numColumns="3"  代表九宫格的列数  auto_fit时为自动

2、实现代码

  1 public class MainActivity extends Activity {
  2 
  3     /** Called when the activity is first created. */
  4 
  5     @Override
  6     protected void onCreate(Bundle savedInstanceState) {
  7         // TODO Auto-generated method stub
  8         super.onCreate(savedInstanceState);
  9         // 设置屏幕没有标题
 10         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
 11         // 去掉标题栏
 12         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 13                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
 14         setContentView(R.layout.activity_main);
 15 
 16         GridView gridview = (GridView) findViewById(R.id.gridview);
 17         // 创建一个数组列表对象
 18         ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
 19 
 20         /**
 21          * 为每个格子添加内容
 22          */
 23         for (int i = 1; i < 10; i++) {
 24             HashMap<String, Object> map = new HashMap<String, Object>();// 建立hashmap对象
 25 
 26             if (i == 1) {
 27                 map.put("ItemImage", R.drawable.g11);
 28                 map.put("ItemText", getResources()
 29                         .getString(R.string.gridview1));
 30             }
 31 
 32             if (i == 2) {
 33                 map.put("ItemImage", R.drawable.g12);
 34                 map.put("ItemText", getResources()
 35                         .getString(R.string.gridview2));
 36             }
 37 
 38             if (i == 3) {
 39                 map.put("ItemImage", R.drawable.g13);
 40                 map.put("ItemText", getResources()
 41                         .getString(R.string.gridview3));
 42             }
 43 
 44             if (i == 4) {
 45                 map.put("ItemImage", R.drawable.g14);
 46                 map.put("ItemText", getResources()
 47                         .getString(R.string.gridview4));
 48             }
 49 
 50             if (i == 5) {
 51                 map.put("ItemImage", R.drawable.g15);
 52                 map.put("ItemText", getResources()
 53                         .getString(R.string.gridview5));
 54             }
 55 
 56             if (i == 6) {
 57                 map.put("ItemImage", R.drawable.g16);
 58                 map.put("ItemText", getResources()
 59                         .getString(R.string.gridview6));
 60             }
 61 
 62             if (i == 7) {
 63                 map.put("ItemImage", R.drawable.g17);
 64                 map.put("ItemText", getResources()
 65                         .getString(R.string.gridview7));
 66             }
 67 
 68             if (i == 8) {
 69                 map.put("ItemImage", R.drawable.g18);
 70                 map.put("ItemText", getResources()
 71                         .getString(R.string.gridview8));
 72             }
 73 
 74             if (i == 9) {
 75                 map.put("ItemImage", R.drawable.g19);
 76                 map.put("ItemText", getResources()
 77                         .getString(R.string.gridview9));
 78             }
 79 
 80             lstImageItem.add(map);
 81         }
 82 
 83         /**
 84          * 为GridView建立SimpleAdapter适配器
 85          */
 86         // SimpleAdapter()中的五个参数分别是:第一个context,第二个数据资源,第三个每一个子项的布局文件,第四个每一个子项中的Key数组
 87         // 第五个每一个子项中的Value数组
 88         SimpleAdapter saImageItems = new SimpleAdapter(this, lstImageItem,
 89                 R.layout.grid_item, new String[] { "ItemImage", "ItemText" },
 90                 new int[] { R.id.ItemImage, R.id.ItemText });
 91         gridview.setAdapter(saImageItems);// 添加适配器
 92         gridview.setOnItemClickListener(new ItemClickListener());// 为每一个子项设置监听
 93     }
 94 
 95     class ItemClickListener implements OnItemClickListener {
 96         @SuppressWarnings("unchecked")
 97         public void onItemClick(AdapterView<?> arg0,// The AdapterView where the
 98                                                     // click happened
 99                 View arg1,// The view within the AdapterView that was clicked
100                 int arg2,// The position of the view in the adapter
101                 long arg3// The row id of the item that was clicked
102         ) {
103             HashMap<String, Object> item = (HashMap<String, Object>) arg0
104                     .getItemAtPosition(arg2);
105             if (item.get("ItemText").equals(
106                     getResources().getString(R.string.gridview1))) {
107                 Toast.makeText(MainActivity.this, R.string.gridview1,
108                         Toast.LENGTH_LONG).show();
109             }
110 
111             if (item.get("ItemText").equals(
112                     getResources().getString(R.string.gridview2))) {
113                 Toast.makeText(MainActivity.this, R.string.gridview2,
114                         Toast.LENGTH_LONG).show();
115             }
116 
117             if (item.get("ItemText").equals(
118                     getResources().getString(R.string.gridview3))) {
119                 Toast.makeText(MainActivity.this, R.string.gridview3,
120                         Toast.LENGTH_LONG).show();
121             }
122 
123             if (item.get("ItemText").equals(
124                     getResources().getString(R.string.gridview4))) {
125                 Toast.makeText(MainActivity.this, R.string.gridview4,
126                         Toast.LENGTH_LONG).show();
127             }
128 
129             if (item.get("ItemText").equals(
130                     getResources().getString(R.string.gridview5))) {
131                 Toast.makeText(MainActivity.this, R.string.gridview5,
132                         Toast.LENGTH_LONG).show();
133             }
134 
135             if (item.get("ItemText").equals(
136                     getResources().getString(R.string.gridview6))) {
137                 Toast.makeText(MainActivity.this, R.string.gridview6,
138                         Toast.LENGTH_LONG).show();
139             }
140 
141             if (item.get("ItemText").equals(
142                     getResources().getString(R.string.gridview7))) {
143                 Toast.makeText(MainActivity.this, R.string.gridview7,
144                         Toast.LENGTH_LONG).show();
145             }
146 
147             if (item.get("ItemText").equals(
148                     getResources().getString(R.string.gridview8))) {
149                 Toast.makeText(MainActivity.this, R.string.gridview8,
150                         Toast.LENGTH_LONG).show();
151             }
152 
153             if (item.get("ItemText").equals(
154                     getResources().getString(R.string.gridview9))) {
155                 Toast.makeText(MainActivity.this, R.string.gridview9,
156                         Toast.LENGTH_LONG).show();
157             }
158         }
159     }
160 
161 }

 

3、实现效果如图所示

 

posted @ 2013-05-09 20:06  蠢驴  阅读(393)  评论(0)    收藏  举报