作用:可使视图左右滑动
main.xml
- 声明一个ViewPager对象
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/pager">
</android.support.v4.view.ViewPager>
</LinearLayout>
view1.xml(view2,view3,view4)
<?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="第一个界面"/>
</LinearLayout>
创建PagerAdapter类型的适配器MyPagerAdapter
实现PagerAdapter的几种方法
//继承PagerAdapter
public class MyPagerAdapter extends PagerAdapter{
private List<View>viewlist;
//创建构造方法
public MyPagerAdapter(List<View>viewlist) {
this.viewlist=viewlist;
}
//返回页卡的数量
@Override
public int getCount() {
return viewlist.size();
}
//View是否来自于对象
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
//实例化一个页卡
@Override
public Object instantiateItem(ViewGroup container, int position) {
//添加到viewGroup中去
container.addView(viewlist.get(position));//添加了一个viewlist所对应的位置的view对象
return viewlist.get(position);
}
//销毁一个页卡
@Override
public void destroyItem(View container, int position, Object object) {
//从container中移除
((ViewGroup) container).removeView(viewlist.get(position));
}
}
MyFragmentPagerAdapter2.java
创建FragmentStatePagerAdapter
package com.example.viewpager1;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.View;
/*
* 因为Fragment是将所有页面都一次性加载进来,所以不存在添加和销毁
*/
public class MyFragmentPagerAdapter2 extends FragmentStatePagerAdapter{
private List<Fragment>fraglist;
private List<String>titlelist;
public MyFragmentPagerAdapter2(FragmentManager fm,List<Fragment>fraglist,List<String> titlelist) {
super(fm);
this.fraglist=fraglist;
this.titlelist=titlelist;
}
@Override
public Fragment getItem(int arg0) {
return fraglist.get(arg0);
}
public String getPageTitle(int position) {
return titlelist.get(position);//返回标题信息
}
@Override
public int getCount() {
return fraglist.size();
}
//销毁
private void onDestory() {
// TODO Auto-generated method stub
}
//创建
@Override
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
return super.instantiateItem(container, position);
}
}
MyFragmentPagerAdapter.java
创建FragmentPagerAdapter
package com.example.viewpager1;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/*
* 因为Fragment是将所有页面都一次性加载进来,所以不存在添加和销毁
*/
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
private List<Fragment>fraglist;
private List<String>titlelist;
public MyFragmentPagerAdapter(FragmentManager fm,List<Fragment>fraglist,List<String> titlelist) {
super(fm);
this.fraglist=fraglist;
this.titlelist=titlelist;
}
@Override
public Fragment getItem(int arg0) {
return fraglist.get(arg0);
}
public String getPageTitle(int position) {
return titlelist.get(position);//返回标题信息
}
@Override
public int getCount() {
return fraglist.size();
}
}
- 声明viewPager的数据源viewlist,fraglistViewPager并初始化
//实现onPageChangeListener接口
public class MainActivity extends FragmentActivity implements OnPageChangeListener{ {
private ViewPager pager;
//声明PagerAdapter的数据源(List<View>)
private List<View>viewlist;
/*声明FragmentPagerAdapter和FragmentStatePagerAdapter
*的数据源List<Fragment>
*/
private List<Fragment>fraglist;
private List<String>titlelist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化ViewPager
pager=(ViewPager) findViewById(R.id.pager);
- 将viewlist作为ViewPager的数据源
//初始化viewlist
viewlist=new ArrayList<View>();
//将布局转化成view对象
/*
* 参数1:上下文
* 参数2:对应布局文件的id
* 参数3:对应的父祖先
*/
View view1=View.inflate(this, R.layout.view1, null);
View view2=View.inflate(this, R.layout.view2, null);
View view3=View.inflate(this, R.layout.view3, null);
View view4=View.inflate(this, R.layout.view4, null);
//将view都放到数据源集合中(将view对象作为viewPager的数据源)
viewlist.add(view1);
viewlist.add(view2);
viewlist.add(view3);
viewlist.add(view4);
- 建立一个PagerAdapte类型的适配器并加载数据源
MyPagerAdapter adapter=new MyPagerAdapter(viewlist);
- viewPager加载适配器
pager.setAdapter(adapter);
- 建立FragmentPagerAdapter和FragmentStatePagerAdapter类型的适配器并加载数据源
//该适配器能自动维护页卡的创建和销毁
MyFragmentPagerAdapter2 adapter3=new MyFragmentPagerAdapter2(getSupportFragmentManager(), fraglist, titlelist);
pager.setOnPageChangeListener(this);
- 将Fragment作为ViewPager的数据源
fraglist=new ArrayList<Fragment>();
fraglist.add(new Fragment1());
fraglist.add(new Fragment2());
fraglist.add(new Fragment3());
fraglist.add(new Fragment4());
//参数1:Fragemnt的管理者
MyFragmentPagerAdapter adapter2=new MyFragmentPagerAdapter(getSupportFragmentManager(), fraglist, titlelist);
//viewPager加载适配器
pager.setAdapter(adapter2);
}
- onPageChangeListener的三个方法
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
//所传进来的参数:position
public void onPageSelected(int arg0) {
Toast.makeText(this, "当前是第"+(arg0+1)+"个页面", Toast.LENGTH_SHORT).show();
}