MainActivity.java
-
public class MainActivity extends Activity
-
{
-
GridView grid;
-
ImageView imageView;
-
int[] imageIds = new int[]
-
{
-
R.drawable.bomb5 , R.drawable.bomb6 , R.drawable.bomb7
-
, R.drawable.bomb8 , R.drawable.bomb9 , R.drawable.bomb10
-
, R.drawable.bomb11 , R.drawable.bomb12 , R.drawable.bomb13
-
, R.drawable.bomb14 , R.drawable.bomb15 , R.drawable.bomb16
-
};
-
@Override
-
public void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
// 创建一个List对象,List对象的元素是Map
-
List<Map<String, Object>> listItems =
-
new ArrayList<Map<String, Object>>();
-
for (int i = 0; i < imageIds.length; i++)
-
{
-
Map<String, Object> listItem = new HashMap<String, Object>();
-
listItem.put("image", imageIds[i]);
-
listItems.add(listItem);
-
}
-
// 获取显示图片的ImageView
-
imageView = (ImageView) findViewById(R.id.imageView);
-
// 创建一个SimpleAdapter
-
SimpleAdapter simpleAdapter = new SimpleAdapter(this,
-
listItems
-
// 使用/layout/cell.xml文件作为界面布局
-
, R.layout.cell, new String[] { "image" },
-
new int[] { R.id.image1 });
-
grid = (GridView) findViewById(R.id.grid01);
-
// 为GridView设置Adapter
-
grid.setAdapter(simpleAdapter);
-
// 添加列表项被选中的监听器
-
grid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
-
{
-
@Override
-
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
-
{
-
// 显示当前被选中的图片
-
imageView.setImageResource(imageIds[position]);
-
}
-
@Override
-
public void onNothingSelected(AdapterView<?> parent)
-
{
-
}
-
});
-
// 添加列表项被单击的监听器
-
grid.setOnItemClickListener(new AdapterView.OnItemClickListener()
-
{
-
@Override
-
public void onItemClick(AdapterView<?> parent, View view,
-
int position, long id)
-
{
-
// 显示被单击的图片
-
imageView.setImageResource(imageIds[position]);
-
}
-
});
-
}
-
}
|
XML文件
Main.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="match_parent"
-
5. android:layout_height="match_parent"
-
6. android:gravity="center_horizontal">
-
7. <!-- 定义一个GridView组件 -->
-
8. <GridView
-
9. android:id="@+id/grid01"
-
10. android:layout_width="match_parent"
-
11. android:layout_height="wrap_content"
-
12. android:horizontalSpacing="1pt"
-
13. android:verticalSpacing="1pt"
-
14. android:numColumns="4"
-
15. android:gravity="center"/>
-
16. <!-- 定义一个ImageView组件 -->
-
17. <ImageView android:id="@+id/imageView"
-
18. android:layout_width="240dp"
-
19. android:layout_height="240dp"
-
20. android:layout_gravity="center_horizontal"/>
-
21.</LinearLayout>
|
Cell.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="horizontal"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:gravity="center_horizontal"
-
android:padding="2pt">
-
<ImageView
-
android:id="@+id/image1"
-
android:layout_width="50dp"
-
android:layout_height="50dp"
-
/>
-
</LinearLayout>
|
效果:
其中关键代码是
-
SimpleAdapter simpleAdapter = new SimpleAdapter(this,
-
listItems
-
// 使用/layout/cell.xml文件作为界面布局
-
, R.layout.cell, new String[] { "image" },
-
new int[] { R.id.image1 });
|
SimpleAdapter的介绍:
第一个参数:是context
第二个参数:是一个List<?extends Map<String,?>>类型的集合对象,该集合中每个Map<String,?>对象生成一个列表项
第三个参数:制定一个界面布局的ID
第四个参数:是一个String[]类型的参数,该参数决定提取Map<String,?>对象中哪些key对应的value来生成列表项
第五个参数:应该是一个int[]类型参数,决定填充哪些组件