BaseAdapter应用 和gallery滑动图片

public class MyActivity extends Activity {
//定变量
    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);
//找到Gallery的id
        gallery=(Gallery)findViewById(R.id.onegallery);
//初始化自定义适配器,并且将图片添加入这个适配器
        myGalleryBase=new MyGalleryBase(mImage,this);
//将gallera改为这个适配器的数据
        gallery.setAdapter(myGalleryBase);
    }
}


        //创建自定义适配器   
public class MyGalleryBase extends BaseAdapter {
//需要图片就创建图片的数组,图片数组为int
    private int[] mImage;
//引用上下文
    private Context mContext;
//讲XML转成view对象
    private LayoutInflater mlayoutInflater;
//构造方法
    public MyGalleryBase(int[] images,Context context){
     //初始化
        mImage=images;
        mContext=context;
        mlayoutInflater=LayoutInflater.from(context);
    }
//显示条目数量
    @Override
    public int getCount() {
        //显示多少次
        return mImage.length;
    }
//
    @Override
    public Object getItem(int position) {
          //返回数据
        return mImage[position];
    }
          
    @Override
    public long getItemId(int position) {
          //返回下标
        return position;
    }

    @Override
//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.setImageResource(mImage[position]);
        return v;
    }
}

//创建Gallery  xml
    <Gallery
            android:id="@+id/onegallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
          //中间的间距
            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;
    }

   public View getView(int position, View convertView, ViewGroup parent) {
      //用v来接收内容id  nrviewlauid为可变的id
       View v=mlayoutInflater.inflate(nrviewlauid,null);
//用v1来接受布局id
 View v1= v.findViewById(layoutid);
     //判断布局的id是否继承与ImageView
        if(v1 instanceof ImageView){
     //将内容存入
        ((ImageView) v1).setImageResource(mImage[position]);
        }
        return v;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值