• Android之GridView(九宫图)


    GridView跟ListView都是比较常用的多控件布局,而GridView更是实现九宫图的首选!本文就是介绍如何使用GridView实现九宫图。GridView的用法很多,网上介绍最多的方法就是自己实现一个ImageAdapter继承BaseAdapter,再供GridView使用,类似这种的方法本文不再重复,本文介绍的GridView用法跟前文ListView的极其类似。。。。也算是我偷懒一下,嘻嘻嘻嘻。。。。

    先来贴出本文代码运行的结果:

    1

     

    本文需要添加/修改3个文件:main.xml、night_item.xml、JAVA源代码。

    main.xml源代码如下,本身是个GirdView,用于装载Item:

     

    1. <?xmlversion="1.0"encoding="utf-8"?>
    2. <GridViewxmlns:android="http://schemas.android.com/apk/res/android"
    3. android:id="@+id/gridview"
    4. android:layout_width="fill_parent"
    5. android:layout_height="fill_parent"
    6. android:numColumns="auto_fit"
    7. android:verticalSpacing="10dp"
    8. android:horizontalSpacing="10dp"
    9. android:columnWidth="90dp"
    10. android:stretchMode="columnWidth"
    11. android:gravity="center"
    12. />

    介绍一下里面的某些属性:

    android:numColumns="auto_fit" ,GridView的列数设置为自动

    android:columnWidth="90dp",每列的宽度,也就是Item的宽度
    android:stretchMode="columnWidth",缩放与列宽大小同步
    android:verticalSpacing="10dp",两行之间的边距,如:行一(NO.0~NO.2)与行二(NO.3~NO.5)间距为10dp
    android:horizontalSpacing="10dp",两列之间的边距。

     

    接下来介绍night_item.xml,这个XML跟前面ListView的ImageItem.xml很类似:

     

    1. <?xmlversion="1.0"encoding="utf-8"?>
    2. <RelativeLayout
    3. xmlns:android="http://schemas.android.com/apk/res/android"
    4. android:layout_height="wrap_content"
    5. android:paddingBottom="4dip"android:layout_width="fill_parent">
    6. <ImageView
    7. android:layout_height="wrap_content"
    8. android:id="@+id/ItemImage"
    9. android:layout_width="wrap_content"
    10. android:layout_centerHorizontal="true">
    11. </ImageView>
    12. <TextView
    13. android:layout_width="wrap_content"
    14. android:layout_below="@+id/ItemImage"
    15. android:layout_height="wrap_content"
    16. android:text="TextView01"
    17. android:layout_centerHorizontal="true"
    18. android:id="@+id/ItemText">
    19. </TextView>
    20. </RelativeLayout>

     

    最后就是JAVA的源代码了,也跟前面的ListView的JAVA源代码很类似,不过多了“选中”的事件处理:

     

    1. publicvoid onCreate(Bundle savedInstanceState) {
    2. super.onCreate(savedInstanceState);
    3. setContentView(R.layout.main);
    4. GridView gridview = (GridView) findViewById(R.id.gridview);
    5. //生成动态数组,并且转入数据
    6. ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
    7. for(int i=0;i<10;i++)
    8. {
    9. HashMap<String, Object> map = new HashMap<String, Object>();
    10. map.put("ItemImage", R.drawable.icon);//添加图像资源的ID
    11. map.put("ItemText", "NO."+String.valueOf(i));//按序号做ItemText
    12. lstImageItem.add(map);
    13. }
    14. //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应
    15. SimpleAdapter saImageItems = new SimpleAdapter(this, //没什么解释
    16. lstImageItem,//数据来源
    17. R.layout.night_item,//night_item的XML实现
    18. //动态数组与ImageItem对应的子项
    19. new String[] {"ItemImage","ItemText"},
    20. //ImageItem的XML文件里面的一个ImageView,两个TextView ID
    21. newint[] {R.id.ItemImage,R.id.ItemText});
    22. //添加并且显示
    23. gridview.setAdapter(saImageItems);
    24. //添加消息处理
    25. gridview.setOnItemClickListener(new ItemClickListener());
    26. }
    27. //当AdapterView被单击(触摸屏或者键盘),则返回的Item单击事件
    28. class ItemClickListener implements OnItemClickListener
    29. {
    30. publicvoid onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened
    31. View arg1,//The view within the AdapterView that was clicked
    32. int arg2,//The position of the view in the adapter
    33. long arg3//The row id of the item that was clicked
    34. ) {
    35. //在本例中arg2=arg3
    36. HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);
    37. //显示所选Item的ItemText
    38. setTitle((String)item.get("ItemText"));
    39. }
    40. }
  • 相关阅读:
    C++中的指针和数组
    windows系统下JDK1.6环境变量配置
    Java Reflection (JAVA反射)
    转载:cin深入分析(下) – cin的错误处理
    OpenGL总结
    OpenGL纹理
    c/C++内存分配
    转载:cin深入分析(上) – cin输入操作处理
    c++中string的用法
    OpenGL颜色
  • 原文地址:https://www.cnblogs.com/Gaojiecai/p/2485565.html
Copyright © 2020-2023  润新知