无论是ArrayAdapter还是SimpleAdapter都继承了BaseAdapter,自定义适配器同样继承BaseAdapter

实例:Gallery实现图片浏览器

<?xml version="1.0" encoding="utf-8"?> <Gallery  xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/gallery"    android:layout_width="match_parent"    android:layout_height="wrap_content" >    </Gallery>

public class MainActivity extends Activity {private Gallery gallery;private int[] res={R.drawable.ic_launcher,R.drawable.ic_launcher};private ImageAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.demo);        gallery = (Gallery) findViewById(R.id.gallery);               adapter = new ImageAdapter(res, this);        gallery.setAdapter(adapter);                            }}

自定义的适配器

public class ImageAdapter  extends BaseAdapter{    public int res[];    private Context context;    public ImageAdapter(int res[],Context context){        this.res=res;        this.context=context;    }        @Override    //返回已定义数据源总数量    public int getCount() {        // TODO Auto-generated method stub        return res.length;    }    @Override    //告诉适配器取得目前容器中的数据对象    public Object getItem(int position) {        // TODO Auto-generated method stub        return position;    }    @Override    //告诉适配器取得目前容器中的数据ID    public long getItemId(int position) {        // TODO Auto-generated method stub        return position;    }    @Override    //取得当前欲显示的图像View    public View getView(int position, View convertView, ViewGroup parent) {        // TODO Auto-generated method stub        ImageView image=new ImageView(context);        image.setBackgroundResource(res[position]);        image.setLayoutParams(new Gallery.LayoutParams(400,300));        image.setScaleType(ScaleType.FIT_XY);        return image;    }}

更多相关文章

  1. android一些认识
  2. Android进度条、自动提示框、下拉框动态数据加载
  3. Android搜索框之PopupWindow+ListView实现
  4. Android中Spinner的用法
  5. Android(安卓)UI控件详解-Gallery(画廊)
  6. Android(安卓)控件之八:AutoCompleteTextView自动完成文本框
  7. Android(安卓)Adapter
  8. android:layout_weight 使用
  9. android UI进阶之仿iphone的tab效果 (Android九宫格)

随机推荐

  1. [Android]Android字体高度的研究
  2. Android中字体颜色的设置
  3. ZZ android am命令
  4. 安卓入门.RelativeLayout相对布局2
  5. android启动模式android:launchMode
  6. android跳转到手机系统默认应用市场的方
  7. android屏幕的那些事之一!
  8. Android(安卓)TabHost的标签放在底部,已写
  9. Android中数据存储的三种方式--基础
  10. android "Only the original thread that