浏览 32248 次
锁定老帖子 主题:Android GridView 例子
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-10-11
下面介绍GridView,先看看效果
1、显示界面
package com.grid.test; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.GridView; public class GridTest extends Activity { private GridView gridview ; private List<GridInfo> list; private GridAdapter adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gridlayout); gridview = (GridView) findViewById(R.id.gridview); list = new ArrayList<GridInfo>(); list.add(new GridInfo("name1")); list.add(new GridInfo("name2")); list.add(new GridInfo("name3")); list.add(new GridInfo("name4")); list.add(new GridInfo("name5")); list.add(new GridInfo("name6")); list.add(new GridInfo("name6")); list.add(new GridInfo("name7")); list.add(new GridInfo("name8")); list.add(new GridInfo("name9")); list.add(new GridInfo("name10")); adapter = new GridAdapter(this); adapter.setList(list); gridview.setAdapter(adapter); } }
2、组件信息
package com.grid.test; /** * Copyright (C) 2010,Under the supervision of China Telecom Corporation * Limited Guangdong Research Institute * The New Vphone Project * @Author fonter.yang * @Create date:2010-10-11 * */ public class GridInfo { private String name; public GridInfo(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
3、Adapter 实现
package com.grid.test; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; /** * Copyright (C) 2010,Under the supervision of China Telecom Corporation Limited * Guangdong Research Institute The New Vphone Project * * @Author fonter.yang * @Create date:2010-10-11 * */ public class GridAdapter extends BaseAdapter { private class GridHolder { ImageView appImage; TextView appName; } private Context context; private List<GridInfo> list; private LayoutInflater mInflater; public GridAdapter(Context c) { super(); this.context = c; } public void setList(List<GridInfo> list) { this.list = list; mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int index) { return list.get(index); } @Override public long getItemId(int index) { return index; } @Override public View getView(int index, View convertView, ViewGroup parent) { GridHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.grid_item, null); holder = new GridHolder(); holder.appImage = (ImageView)convertView.findViewById(R.id.itemImage); holder.appName = (TextView)convertView.findViewById(R.id.itemText); convertView.setTag(holder); }else{ holder = (GridHolder) convertView.getTag(); } GridInfo info = list.get(index); if (info != null) { holder.appName.setText(info.getName()); } return convertView; } }
4、单个组件的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" android:src="@drawable/icon"> </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>
5、显示界面的XML
<?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" />
6、源码下载
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-10-13
我一直觉得GridView 还存在很多问题
|
|
返回顶楼 | |
发表时间:2010-10-13
aa87963014 写道 我一直觉得GridView 还存在很多问题
什么问题? |
|
返回顶楼 | |
发表时间:2011-03-26
这样的话,事件怎么添加进去呢?
|
|
返回顶楼 | |
发表时间:2011-03-26
boygirl 写道 这样的话,事件怎么添加进去呢?
GridAdapter的getView里 |
|
返回顶楼 | |
发表时间:2011-03-30
还没尝试过GridView的布局方式呢 thanks
|
|
返回顶楼 | |
发表时间:2011-03-30
boygirl 写道 这样的话,事件怎么添加进去呢?
setAdapter(); setOnItemClickListener();这样应该行 |
|
返回顶楼 | |
发表时间:2011-08-24
GridHolder 我的SDK里面怎么没有这个类啊? 我的是 2.1 的。。为什么呢?
|
|
返回顶楼 | |
发表时间:2011-08-24
zhangxpower 写道 GridHolder 我的SDK里面怎么没有这个类啊? 我的是 2.1 的。。为什么呢?
GridHolder 是新建的。你看看GridAdapter里就有的。。 |
|
返回顶楼 | |