viewpager

package com.dts.zgsc.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.dts.zgsc.R;
import com.dts.zgsc.customview.photoview.PhotoView;
import com.squareup.picasso.Picasso;

public class ViewPagerPhotoViewDemo extends Activity{
	private ViewPager mViewPager;
	  String[] urls = new String[]{

	            "http://a.hiphotos.baidu.com/image/pic/item/3bf33a87e950352ad6465dad5143fbf2b2118b6b.jpg",
	            "http://a.hiphotos.baidu.com/image/pic/item/c8177f3e6709c93d002077529d3df8dcd0005440.jpg",
	            "http://f.hiphotos.baidu.com/image/pic/item/7aec54e736d12f2ecc3d90f84dc2d56285356869.jpg",
	            "http://e.hiphotos.baidu.com/image/pic/item/9c16fdfaaf51f3de308a87fc96eef01f3a297969.jpg",
	            "http://d.hiphotos.baidu.com/image/pic/item/f31fbe096b63f624b88f7e8e8544ebf81b4ca369.jpg",
	            "http://h.hiphotos.baidu.com/image/pic/item/11385343fbf2b2117c2dc3c3c88065380cd78e38.jpg",
	            "http://c.hiphotos.baidu.com/image/pic/item/3801213fb80e7bec5ed8456c2d2eb9389b506b38.jpg"

	    };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_viewpager_photoview_layout);
		
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		MyViewPagerAdapter adapter = new MyViewPagerAdapter(this);
		adapter.setData(urls);
		
		mViewPager.setAdapter(adapter);
		
	}
	
	private class MyViewPagerAdapter extends PagerAdapter{
		
		private String[] data = {};
		private Context context;
		
		public MyViewPagerAdapter(Context context){
			this.context = context;
		}
		
		public void setData(String[] data){
			this.data = data;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.length;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub
			PhotoView photo = new PhotoView(context);
			Picasso.with(context).load(data[position]).placeholder(R.drawable.ic_launcher).into(photo);
			container.addView(photo);
			
			return photo;
		}
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			//container.removeView(mViewPager.getChildAt(position));

			 container.removeView((View) object);
		}
		
	}

}

你可能感兴趣的:(viewpager)