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

Posted 曹小小曹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用GridView和SimpleAdapter实现手机界面常见的九宫格相关的知识,希望对你有一定的参考价值。

首先是两个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实现手机界面常见的九宫格的主要内容,如果未能解决你的问题,请参考以下文章

Android--Gridview使用SimpleAdapter加载bitmap图片

Android--Gridview使用SimpleAdapter加载bitmap图片

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

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

GridView使用SimpleAdapter

gridview NullPointerException simpleAdapter