Android:ViewPager
下面是一个实例,
目录层次结构如下:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > </android.support.v4.view.ViewPager> </LinearLayout>
view01.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="the first" /> </LinearLayout>
view02.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="the first" /> </LinearLayout>
view03,view04略。
MyPagerAdapter.java:
package com.wyl.veiwpager; import java.util.List; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; public class MyPagerAdapter extends PagerAdapter { private List<View> viewList; private List<String> titleList; /* * 建立一个构造方法: */ public MyPagerAdapter(List<View> viewList) { // ,List<String> titleList this.viewList = viewList; // this.titleList = titleList; } // 返回所有视图的数量 @Override public int getCount() { // TODO Auto-generated method stub return viewList.size(); } // 判断视图是否由对象产生 @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 container.addView(viewList.get(position)); return viewList.get(position); } // 删除页面 @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(viewList.get(position)); } @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titleList.get(position); } }
MainActivity:
package com.wyl.veiwpager; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.view.View; public class MainActivity extends Activity { private List<View> viewlist; private ViewPager pager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //初始化viewlist viewlist = new ArrayList<View>(); /* * 第二个参数为:布局文件的layoutId * 第三个参数: 父组件,一般写为null就可以了 */ View view01 = View.inflate(this, R.layout.view01, null); View view02 = View.inflate(this, R.layout.view02, null); View view03 = View.inflate(this, R.layout.view03, null); View view04 = View.inflate(this, R.layout.view04, null); /* * 通过view对象作为ViewPager的数据源 */ viewlist.add(view01); viewlist.add(view02); viewlist.add(view03); viewlist.add(view04); //1 初始化ViewPager pager= (ViewPager) findViewById(R.id.pager); //2 创建PagerAdapter的适配器 MyPagerAdapter adapter = new MyPagerAdapter(viewlist); //3 ViewPager加载适配器 pager.setAdapter(adapter); } }
效果图如下,