android,gridview
package com.wes.gridview; import java.util.List; import android.content.Context; import android.content.pm.ResolveInfo; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class MyAdapter extends BaseAdapter { Context context; //上下文关系 List<ResolveInfo> apps; //数据源 public MyAdapter(Context ctx,List<ResolveInfo> apps) { //构造函数 this.context = ctx; this.apps = apps; } @Override public int getCount() { return apps.size(); //得到个数 } @Override public Object getItem(int position) { return apps.get(position); //得到该位置的Item } @Override public long getItemId(int position) { return position; //得到Item的位置 } @Override public View getView(int position, View convertView, ViewGroup parent) { //得到视图 ImageView imageView; if (convertView == null) { imageView = (ImageView) new ImageView(context); //实例化一个ImageView imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);//设置尺度模式,缩放以处于中间 imageView.setLayoutParams(new GridView.LayoutParams(50, 50));//设置布局参数 } else { imageView = (ImageView) convertView; //直接返回converView } imageView.setTag(position); ResolveInfo info = apps.get(position); //得到该位置的ResolvsInfo //为ImageView设置背景图片 imageView.setImageDrawable(info.activityInfo.loadIcon(context.getPackageManager())); return imageView; } }
getView会创建立一个屏幕的View(跟屏幕大小有关),接着当用户拖动平面时getView会把已经创建的view传递进来,这里需要做的事情就是改变view的内容
参考:
http://www.oschina.net/question/54100_27825