当前位置: 移动技术网 > IT编程>开发语言>Java > android中GridView的用法示例

android中GridView的用法示例

2019年07月22日  | 移动技术网IT编程  | 我要评论

在android程序设计中gridview跟listview都是比较常用的多控件布局,而gridview更是实现九宫图的首选!本文就是介绍如何使用gridview实现九宫图。gridview的用法很多,网上介绍最多的方法就是自己实现一个imageadapter继承baseadapter,再供gridview使用,类似这种的方法本文不再重复,本文介绍的gridview用法跟之前介绍过的listview极其类似。

我们先来看看本文代码运行的结果:

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

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

<?xml version="1.0" encoding="utf-8"?>
<gridview xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/gridview"
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"
  android:numcolumns="auto_fit"
  android:verticalspacing="10dp"
  android:horizontalspacing="10dp"
  android:columnwidth="90dp"
  android:stretchmode="columnwidth"
  android:gravity="center"
/>

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

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很类似:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="wrap_content" 
     android:paddingbottom="4dip" android:layout_width="fill_parent">
     <imageview 
        android:layout_height="wrap_content" 
        android:id="@+id/itemimage" 
        android:layout_width="wrap_content" 
        android:layout_centerhorizontal="true"> 
     </imageview>
     <textview 
        android:layout_width="wrap_content" 
        android:layout_below="@+id/itemimage" 
        android:layout_height="wrap_content" 
        android:text="textview01" 
        android:layout_centerhorizontal="true" 
        android:id="@+id/itemtext">
     </textview>
</relativelayout>

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

public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
gridview gridview = (gridview) findviewbyid(r.id.gridview);

//生成动态数组,并且转入数据
arraylist<hashmap<string, object>> lstimageitem = new arraylist<hashmap<string, object>>();
for(int i=0;i<10;i++)
{
 hashmap<string, object> map = new hashmap<string, object>();
 map.put("itemimage", r.drawable.icon);//添加图像资源的id
 map.put("itemtext", "no."+string.valueof(i));//按序号做itemtext
 lstimageitem.add(map);
}
//生成适配器的imageitem <====> 动态数组的元素,两者一一对应
simpleadapter saimageitems = new simpleadapter(this, //没什么解释
     lstimageitem,//数据来源 
     r.layout.night_item,//night_item的xml实现
     
     //动态数组与imageitem对应的子项    
     new string[] {"itemimage","itemtext"}, 
     
     //imageitem的xml文件里面的一个imageview,两个textview id
     new int[] {r.id.itemimage,r.id.itemtext});
//添加并且显示
gridview.setadapter(saimageitems);
//添加消息处理
gridview.setonitemclicklistener(new itemclicklistener());
}

//当adapterview被单击(触摸屏或者键盘),则返回的item单击事件
class itemclicklistener implements onitemclicklistener
{
 public void onitemclick(adapterview<?> arg0,//the adapterview where the click happened 
    view arg1,//the view within the adapterview that was clicked
    int arg2,//the position of the view in the adapter
    long arg3//the row id of the item that was clicked
    ) {
 //在本例中arg2=arg3
 hashmap<string, object> item=(hashmap<string, object>) arg0.getitematposition(arg2);
 //显示所选item的itemtext
 settitle((string)item.get("itemtext"));
 }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网