博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
22 GridView 02
阅读量:5222 次
发布时间:2019-06-14

本文共 3839 字,大约阅读时间需要 12 分钟。

GridView 一种是直接用simpleAdapt,另一种是自己写一个myAdapt 继承baseAdapt.这里是第二个例子。

http://fonter.iteye.com/blog/781403

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
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
(); 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); }}
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
list; private LayoutInflater mInflater; public GridAdapter(Context c) { super(); this.context = c; } public void setList(List
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

5显示界面的XML

 

转载于:https://www.cnblogs.com/cliffhuang/p/3704514.html

你可能感兴趣的文章
faceswap安装说明
查看>>
【转】OpenCV对图片中的RotatedRect进行填充
查看>>
pymysql模块
查看>>
API查询利器-Zeal
查看>>
c#运算符
查看>>
C# 之泛型详解
查看>>
leetcode -- 一个算法面试题相关的网站
查看>>
Reentrancy VS Thread safe
查看>>
多表insert操作详解
查看>>
关于Tcp的一些浅见
查看>>
三次握手四次挥手
查看>>
HTML5触摸事件演化tap事件
查看>>
git使用记录
查看>>
国外物联网平台初探(二) ——微软Azure IoT
查看>>
Java中集合Set的用法
查看>>
Android瀑布流照片墙实现,体验不规则排列的美感
查看>>
Swift - 使用arc4random()、arc4random_uniform()取得随机数
查看>>
android Json解析详解
查看>>
Android应用程序窗口(Activity)与WindowManagerService服务的连接过程分析
查看>>
《大道至简》第五章读后感
查看>>