使用PageTransformer时递归进入executePendingTransactions

时间:2014-06-30 04:49:19

标签: android android-fragments

我正在直接使用教程here.中的ZoomOutPageTransformer。我有一个导航抽屉,抽屉上的第一个项目是ViewPager片段。这在第一次尝试时工作正常但在我移动到另一个活动并返回之后,它会抛出此错误:

06-29 21:38:06.567    5905-5905/com.hidden.hidden E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.hidden.hidden, PID: 5905
    java.lang.IllegalStateException: Recursive entry to executePendingTransactions
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1456)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
            at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.setPageTransformer(ViewPager.java:623)
            at com.hidden.hidden.MyFragment.onActivityCreated(MyFragment.java:56)
            at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1512)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:962)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

我的代码

MyFragment.java中的onActivityCreated()

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mPager = (ViewPager) getView().findViewById(R.id.pager);
    mPager.setAdapter(new MyCustomAdapter(getFragmentManager()));

    //Line 56
    mPager.setPageTransformer(true, new ZoomOutPageTransformer());

    mIndicator = (LinePageIndicator) getView().findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);
}

ZoomOutPageTransformer.java

public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
    private static final float MIN_SCALE = 0.85f;
    private static final float MIN_ALPHA = 0.5f;

    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();
        int pageHeight = view.getHeight();

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);

        } else if (position <= 1) { // [-1,1]
            // Modify the default slide transition to shrink the page as well
            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            float horzMargin = pageWidth * (1 - scaleFactor) / 2;
            if (position < 0) {
                view.setTranslationX(horzMargin - vertMargin / 2);
            } else {
                view.setTranslationX(-horzMargin + vertMargin / 2);
            }

            // Scale the page down (between MIN_SCALE and 1)
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

            // Fade the page relative to its size.
            view.setAlpha(MIN_ALPHA +
                    (scaleFactor - MIN_SCALE) /
                            (1 - MIN_SCALE) * (1 - MIN_ALPHA));

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setAlpha(0);
        }
    }
}

同样,变换器直接来自Android Developer的页面。

非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

I had the same issue while using PageTransformer. In my case, the solution was to call pager.setPageTransformer() BEFORE setting adapter to my viewPager.

cardsPager.setPageTransformer(true, new CardsPageTransformer());
cardsPager.setAdapter(new CardsPagerAdapter(getFragmentManager()));