android.support.v4.view.ViewPager
左右切换当前的view,实现滑动切换的效果。
建立ViewPage的步骤: 1. 在XML布局中加入android.support.v4.view.ViewPager:
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
2. 在MainActivity加载要显示的页卡:
当需要加载的页卡是View时:
LayoutInflater lf = getLayoutInflater().from(this);
view1 = lf.inflate(R.layout.layout1, null);
view2 = lf.inflate(R.layout.layout2, null);
view3 = lf.inflate(R.layout.layout3, null);
viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
当需要加载的页卡是Fragment时:
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
Fragment3 fragment3 = new Fragment3();
Fragment4 fragment4 = new Fragment4();
// 将要分页显示的View装入数组中
List<Fragment> list = new ArrayList<Fragment>();
list.add(fragment1);
list.add(fragment2);
list.add(fragment3);
list.add(fragment4);
3. 用相应的适配器Adapter关联上面的页卡(View/Fragment)和ViewPager:
写一个继承相应Adapter的类,然后根据需求重写方法,有几个方法是必须重写的
当页卡是View时:用ViewPagerAdapter:
public class MyViewPagerAdapter extends PagerAdapter{
private List<View> mListViews;
public MyViewPagerAdapter(List<View> mListViews) {
this.mListViews = mListViews;//构造方法,参数是我们的页卡,这样比较方便。
}
//直接继承PagerAdapter,至少必须重写下面的四个方法,否则会报错
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mListViews.get(position));//删除页卡
}
@Override
public Object instantiateItem(ViewGroup container, int position){
//这个方法用来实例化页卡
container.addView(mListViews.get(position), 0);//添加页卡
return mListViews.get(position);
}
@Override
public int getCount() {
return mListViews.size();//返回页卡的数量
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;//官方提示这样写
}
}
当页卡是Fragment时:用FragmentAdapter:
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentAdapter extends FragmentPagerAdapter {
List<Fragment> list;
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
}
public MyFragmentAdapter(FragmentManager fm,List<Fragment> list) {
super(fm);
this.list=list;
}//写构造方法,方便赋值调用
@Override
public Fragment getItem(int arg0) {
return list.get(arg0);
}//根据Item的位置返回对应位置的Fragment,绑定item和Fragment
@Override
public int getCount() {
return list.size();
}//设置Item的数量
}
4. 在Activity里绑定Adapter
ViewPagerAdapter:
private ViewPager viewPager;
viewPager.setAdapter(new MyViewPagerAdapter(views));
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(new MyOnPageChangeListener());//设置页面切换时候的监听器(可选,用了之后要重写它的回调方法处理页面切换时候的事务)
FragmentPagerAdapter:
vp.addOnPageChangeListener(this);//设置页面切换时的监听器(可选,用了之后要重写它的回调方法处理页面切换时候的事务)
vp.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), list)
5. 设置切换、滑动动画 利用Viewpage自带的方法setPageTransformer()可用于设置切换动画
public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.75f;
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);
} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE
+ (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
mViewPager.setPageTransformer(true, new DepthPageTransformer());
这样,切换动画的设置就完成了。
当然,你也可以自定义切换动画的效果,具体请点击这里
关于ViewPage的滑动接口**OnPageChangeListener()**如何使用,请看我写的另外一篇文章:Android开发:ViewPage滑动接口最详细解析
即当拥有大量的页面时,使用FragmentStatePagerAdapter不必在内存中占用大量的内存