我正在使用带有 Scrollview 淡入动画的 viewPager 片段。为此,我在 viewpager.pagetransformer 上添加了该功能。当我添加此功能时,滚动视图无法正常工作(仅第一步数据滚动)。
关于viewpager的声明:
mViewPager = (ViewPager) findViewById(R.id.pager);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setPageTransformer(true, new CrossfadePageTransformer());
mViewPager.setAdapter(mSectionsPagerAdapter);
SectionsPagerAdapter 类是:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class
// below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
以及用于淡入淡出动画的 CrossfadePageTransformer 类:
public class CrossfadePageTransformer implements ViewPager.PageTransformer {
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@SuppressLint("NewApi")
@Override
public void transformPage(View view, float position) {
//working.....
view.setScrollY(0);
view.setTranslationX(view.getWidth() * -position);
if (position <= -1.0F || position >= 1.0F) {
view.setAlpha(0.0F);
} else if (position == 0.0F) {
view.setAlpha(1.0F);
} else {
// position is between -1.0F & 0.0F OR 0.0F & 1.0F
view.setAlpha(1.0F - Math.abs(position));
}
}
我该如何解决这个问题?请给我建议。谢谢大家。