Scrollview无法在viewpager.pagetransformer

时间:2015-07-27 13:04:17

标签: android android-fragments android-viewpager scrollview android-pagetransformer

我正在使用带有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));
    }
}

我如何解决这个问题?请向我建议。谢谢。

1 个答案:

答案 0 :(得分:0)

事情ViewPager也实现onTouchEvent,与ScrollView相同,因此它们相互干扰。

我担心没有简单的方法让它工作,所以不要使用嵌套的滚动视图。