//滑动切换界面
private ViewPager viewPager;
viewPager.setAdapter(Myadter);
//主要是为了获取position(滑动到了第几页)
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
Log.d("vivi", "onPageScrollStateChanged: " + state);
switch(state) {
case ViewPager.SCROLL_STATE_DRAGGING:
//拖的时候才进入下一页
flag = false;
Log.d("vivi", "SCROLL_STATE_DRAGGING: " + ViewPager.SCROLL_STATE_DRAGGING);
break;
case ViewPager.SCROLL_STATE_SETTLING:
flag = true;
Log.d("vivi", "SCROLL_STATE_SETTLING: " + ViewPager.SCROLL_STATE_SETTLING);
break;
case ViewPager.SCROLL_STATE_IDLE:
Log.d("vivi", "SCROLL_STATE_IDLE: " + ViewPager.SCROLL_STATE_IDLE + " mViewPager.getCurrentItem() " + viewPager.getCurrentItem());
/**
* 判断是不是最后一页,同是是不是拖的状态
*/
if (viewPager.getCurrentItem() == Myadter.getCount() - 1 && !flag) {
Intent localIntent = new Intent();
localIntent.setClass(main_content.this, CommitActivity.class);
startActivity(localIntent);
// overridePendingTransition(0, 0);
finish();
}
flag = true;
break;
}
}
});
//滑动
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//代表选择哪一个页面
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
//==2是滑动的意思
if(state==2){
//获得当前的页数
int a=viewPager.getCurrentItem()+1;
textView.setText(a+"/"+fragments.size());
}
}
});
}
//滑动改变
FragmentManager fr=getSupportFragmentManager();
FragmentPagerAdapter Myadter =new FragmentPagerAdapter(fr){
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
};