ViewPager
我使用and创建了一个 android 应用程序Fragments
。然后我想使用动画在彼此之间转换一些片段。我
PageTransformer
为此选择。我PageTransformer
与ViewPager
.
这是我的MainActivity类pageTransformer
代码,
public class MainActivity extends FragmentActivity {
private ViewPager viewPager;
private int counterNumber=-1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager)findViewById(R.id.viewPager);
viewPager.setAdapter(new MyPagerAdapter2(getSupportFragmentManager()));
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (viewPager.getCurrentItem() == 0) {
counterNumber=0;
return true;
}
if (viewPager.getCurrentItem() == 1) {
counterNumber=1;
return true;
}
if (viewPager.getCurrentItem() == 2) {
counterNumber=2;
return true;
}
if (viewPager.getCurrentItem() == 3) {
counterNumber=3;
return true;
}
if (viewPager.getCurrentItem() == 4) {
counterNumber=4;
return true;
}
return false;
}
});
viewPager.setPageTransformer(false, new ViewPager.PageTransformer() {
@Override
public void transformPage(View page, float position) {
page.setRotation(position*-100);
}
});
}
private class MyPagerAdapter2 extends FragmentPagerAdapter {
public MyPagerAdapter2(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: counterNumber=0; return FirstFragment1.newInstance("FirstFragment_1");
case 1: counterNumber=1;return SecondFragment1.newInstance("SecondFragment_1");
case 2: counterNumber=2;return ThirdFragment1.newInstance("ThirdFragment_1");
case 3: counterNumber=3;return FourthFragment1.newInstance("FourthFragment_1");
case 4: counterNumber=4;return FifthFragment1.newInstance("FifthFragment_1");
default: return FirstFragment1.newInstance("DefaultFragment_1");
}
}
@Override
public int getCount() {
return 5;
}
}
public void setCurrentItem(int which) {
if(viewPager != null && which >= 0 && which <= 4) {
viewPager.setCurrentItem(which);
}
}
}
但我面临一个问题。当我应用它PageTransformer
时,它会影响所有片段。但我想将它用于特定的片段。
我怎么能做到呢?有什么想法吗?
谢谢你 。