public class MyActivity extends Activity {
//定变量
private Gallery gallery;
private MyGalleryBase myGalleryBase;
private Gallery gallery;
private MyGalleryBase myGalleryBase;
//创建int类型的图片数组,将图片都添加进去
private int[] mImage=new int[]{R.drawable.chitong,R.drawable.proxy,R.drawable.quanyuan,R.drawable.thumb,R.drawable.tong};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
private int[] mImage=new int[]{R.drawable.chitong,R.drawable.proxy,R.drawable.quanyuan,R.drawable.thumb,R.drawable.tong};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//找到Gallery的id
gallery=(Gallery)findViewById(R.id.onegallery);
gallery=(Gallery)findViewById(R.id.onegallery);
//初始化自定义适配器,并且将图片添加入这个适配器
myGalleryBase=new MyGalleryBase(mImage,this);
myGalleryBase=new MyGalleryBase(mImage,this);
//将gallera改为这个适配器的数据
gallery.setAdapter(myGalleryBase);
}
}
gallery.setAdapter(myGalleryBase);
}
}
//创建自定义适配器
public class MyGalleryBase extends BaseAdapter {
//需要图片就创建图片的数组,图片数组为int
private int[] mImage;
private int[] mImage;
//引用上下文
private Context mContext;
private Context mContext;
//讲XML转成view对象
private LayoutInflater mlayoutInflater;
private LayoutInflater mlayoutInflater;
//构造方法
public MyGalleryBase(int[] images,Context context){
public MyGalleryBase(int[] images,Context context){
//初始化
mImage=images;
mContext=context;
mlayoutInflater=LayoutInflater.from(context);
}
mImage=images;
mContext=context;
mlayoutInflater=LayoutInflater.from(context);
}
//显示条目数量
@Override
public int getCount() {
@Override
public int getCount() {
//显示多少次
return mImage.length;
}
//
@Override
public Object getItem(int position) {
return mImage.length;
}
//
@Override
public Object getItem(int position) {
//返回数据
return mImage[position];
}
@Override
public long getItemId(int position) {
return mImage[position];
}
@Override
public long getItemId(int position) {
//返回下标
return position;
}
@Override
return position;
}
@Override
//int position 下标 View convertView 布局的对象 ViewGroup parent父类对象
public View getView(int position, View convertView, ViewGroup parent) {
public View getView(int position, View convertView, ViewGroup parent) {
//View v可以不用定义 使用View convertView 都代表布局对象
View v=mlayoutInflater.inflate(R.layout.view_gallery,null);
ImageView imageView=(ImageView)v.findViewById(R.id.oneimageview);
ImageView imageView=(ImageView)v.findViewById(R.id.oneimageview);
//导入图片为这个位置
imageView.setImageResource(mImage[position]);
return v;
}
}
imageView.setImageResource(mImage[position]);
return v;
}
}
//创建Gallery xml
<Gallery
android:id="@+id/onegallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/onegallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
//中间的间距
android:spacing="10dp">
</Gallery>
android:spacing="10dp">
</Gallery>
//创建判断 地址 实现复用
//布局id
private int layoutid;
//存放内容id
private int nrviewlauid;
public MyGalleryBase(int[] images,Context context,int layoutid,int nrviewlauid){
mImage=images;
mContext=context;
mlayoutInflater=LayoutInflater.from(context);
this.layoutid=layoutid;
this.nrviewlauid=nrviewlauid;
}
private int nrviewlauid;
public MyGalleryBase(int[] images,Context context,int layoutid,int nrviewlauid){
mImage=images;
mContext=context;
mlayoutInflater=LayoutInflater.from(context);
this.layoutid=layoutid;
this.nrviewlauid=nrviewlauid;
}
public View getView(int position, View convertView, ViewGroup parent) {
//用v来接收内容id nrviewlauid为可变的id
View v=mlayoutInflater.inflate(nrviewlauid,null);
View v=mlayoutInflater.inflate(nrviewlauid,null);
//用v1来接受布局id
View v1= v.findViewById(layoutid);
//判断布局的id是否继承与ImageView
if(v1 instanceof ImageView){
if(v1 instanceof ImageView){
//将内容存入
((ImageView) v1).setImageResource(mImage[position]);
}
return v;
}
((ImageView) v1).setImageResource(mImage[position]);
}
return v;
}