关闭

Android 图片滑动效果: ViewPager

112人阅读 评论(0) 收藏 举报
分类:

xml文件中:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.picmove.MainActivity" >

	<android.support.v4.view.ViewPager
	    android:id="@+id/viewpager"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent" 
	    />    

</RelativeLayout>


MainActivity中:


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ViewPager mviewPager = (ViewPager) findViewById(R.id.viewpager);

		mviewPager.setAdapter(new showPictureAdapter(this));
	}

	private class showPictureAdapter extends PagerAdapter {

		private Context context;
		private int[] images = new int[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e };
		private List<ImageView> list;
		private int clickCount = 0;

		public showPictureAdapter(Context context) {
			this.context = context;
			list = new ArrayList<ImageView>();
			for (int i = 0; i < images.length; i++) {
				if (clickCount > 4) {
					clickCount = 0;
				}
				ImageView imageView = new ImageView(context);
				imageView.setImageResource(images[clickCount]);
				clickCount++;
				list.add(imageView);
			}
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(list.get(position));

		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(list.get(position));
			
			return list.get(position);
		}

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

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

	}
}


   
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:46847次
    • 积分:1465
    • 等级:
    • 排名:千里之外
    • 原创:103篇
    • 转载:6篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论