第六节 ViewPager

笔记待写~

运行结果截图:


源代码:

MyViewPager.java

package com.example.myviewpager;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyViewPagerActivity extends Activity {

	private ViewPager mViewPager;
	private List mList;	
	private LayoutInflater inflater;
	private MyAdapter mAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myviewpager);		
		mViewPager = (ViewPager) findViewById(R.id.viewpager);		
		//把xml文件转换成View
		mList = new ArrayList();//每一项都是一个布局文件
		inflater = LayoutInflater.from(this);
		mList.add(inflater.inflate(R.layout.page_1, null));
		mList.add(inflater.inflate(R.layout.page_2, null));
		mList.add(inflater.inflate(R.layout.page_3, null));
		mList.add(inflater.inflate(R.layout.page_4, null));				
		mAdapter = new MyAdapter();
		mViewPager.setAdapter(mAdapter);
	}
	
	class MyAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object obj) {
			return view == obj;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			//super.destroyItem(container, position, object);
			((ViewPager)container).removeView((View)object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			((ViewPager)container).addView(mList.get(position));
			return mList.get(position);
		}		
	}
}
activity_myviewpager.xml
还有布局文件如page_1.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/a" >

</ImageView>



阅读更多
文章标签: viewpager
想对作者说点什么? 我来说一句

ViewPager Demo

2011年08月01日 21KB 下载

viewpagerdeom

2017年12月02日 1.86MB 下载

没有更多推荐了,返回首页

不良信息举报

第六节 ViewPager

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭