码迷,mamicode.com
首页 > 移动开发 > 详细

使用GridView和SimpleAdapter实现手机界面常见的九宫格

时间:2017-05-08 23:31:14      阅读:440      评论:0      收藏:0      [点我收藏+]

标签:使用   show   hash   package   事件   super   name   监听器   界面   

首先是两个XML界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <GridView
        android:id="@+id/MyGridView"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:stretchMode="columnWidth"
        android:numColumns="3" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/ItemImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:layout_gravity="center"/>
    <TextView
        android:id="@+id/ItemTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"/>

</LinearLayout>

然后是Java实现程序:

 1 package com.example.zhoushasha.myapplication;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.view.Window;
 7 import android.view.WindowManager;
 8 import android.widget.AdapterView;
 9 import android.widget.GridView;
10 import android.widget.SimpleAdapter;
11 import android.widget.Toast;
12 
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 
17 public class MainActivity extends AppCompatActivity {
18     private GridView mGridView;
19 
20 
21 
22     private int[] imageRes = { R.drawable.tu1, R.drawable.tu2,
23             R.drawable.tu3,R.drawable.ty4,R.drawable.tu5,R.drawable.tu6,R.drawable.tu7,R.drawable.tu8,R.drawable.tu9 };
24 
25 
26 
27     private String[] itemName = { "图1", "图2", "图3", "图4", "图5 ",
28 
29             "图6", "图7 ", "图8", "图9"  };
30 
31     @Override
32 
33     public void onCreate(Bundle savedInstanceState) {
34 
35         super.onCreate(savedInstanceState);
36         setContentView(R.layout.activity_main);
37 
38 
39 
40         mGridView = (GridView) findViewById(R.id.MyGridView);
41 
42         List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
43 
44         int length = itemName.length;
45 
46         for (int i = 0; i < length; i++) {
47 
48             HashMap<String, Object> map = new HashMap<String, Object>();
49 
50             map.put("ItemImageView", imageRes[i]);
51 
52             map.put("ItemTextView", itemName[i]);
53 
54             data.add(map);
55 
56         }
57 
58         //为itme.xml添加适配器
59 
60         SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,
61 
62                 data, R.layout.item, new String[] { "ItemImageView","ItemTextView" },
63                 new int[] { R.id.ItemImageView,R.id.ItemTextView });
64 
65         mGridView.setAdapter(simpleAdapter);
66 
67         //为mGridView添加点击事件监听器
68 
69         mGridView.setOnItemClickListener(new GridViewItemOnClick());
70 
71     }
72 
73     //定义点击事件监听器
74 
75     public class GridViewItemOnClick implements AdapterView.OnItemClickListener {
76 
77         @Override
78 
79         public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
80 
81             Toast.makeText(getApplicationContext(), position + "",
82 
83                     Toast.LENGTH_SHORT).show();
84 
85         }
86 
87     }
88 
89 }

运行就可以实现功能

使用GridView和SimpleAdapter实现手机界面常见的九宫格

标签:使用   show   hash   package   事件   super   name   监听器   界面   

原文地址:http://www.cnblogs.com/zhoushasha/p/6828147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!