Viewpager Infinity从第1页开始更改 - > 2,2不膨胀

时间:2015-03-24 06:40:39

标签: android android-viewpager fragmentstatepageradapter

搜索了很多方法,将viewpager带到无穷远处。我找到了这样做的方式:

  • 设置数据的数量为max(Integer.MAX_VALUE)
  • getItem方法()中的
  • :返回数据%CURRENT_DATA_SIZE。 (我的片段是6)

好吧那么它从左到右通常是无限的。例如:

  • 我在Viewpager中有6个片段,分别叫做A,B,C,D,E,F。我从A-> F或F - >滑动通常。
  • 然后我找到了从A - >滑动的方法。 F"在viewpager"中正常滑动,例如B - > C,C - > d。
  • 通过setCurrentItem(1200),然后我有无限Viewpager,但主要问题是:
    • 来自A - > B,当页面改为B时,似乎片段B被创建但只有BLACK View,可能这个视图没有被膨胀(通过onCreate方法?)

这是我的照片(我在Viewpager中的订单片段:A,B,C,D,E,F)

来自A - > F:通常光滑 enter image description here

来自A - > B:B是BLACK VIEW? enter image description here

从B-> C enter image description here  剩下的东西都可以,当来自:D-> C-> B时,则B正常显示。这对我来说是个大问题......

这是我的View寻呼机:

mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
            int pos;
            float temp = -1;

            @Override
            public void onPageSelected(int position) {

                if (position == pos) {
                    Logger.error("onPageSelected position old " + position);
                } else {
                    // mViewPager.setCurrentItem(position, false);
                    Logger.error("mViewPager.getChildAt(position) "
                            + mViewPager.getChildAt(position + 1) == null ? "null cmnr"
                            : "null deo dau?");
                    if (mViewPager.getChildAt(position) != null) {
                        // mViewPager.getChildAt(position).bringToFront();
                    }
                    Logger.error("onPageSelected position new " + position);
                }
            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                    int positionOffsetPixels) {
                focusedPage = position;
                boolean isChange = true;
                if (isChange)
                    if (focusedPage == 0) {
                        mViewPager.setCurrentItem(12, false);
                        isChange = false;
                    }

                textViewTime.setAlpha(1 - positionOffset);
                temp = positionOffset;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_DRAGGING) {
                }

                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    pos = focusedPage;

                }
                if (state == ViewPager.SCROLL_STATE_SETTLING) {
                }
            }
        });

然后我的适配器:

public class PagerAdapter extends FragmentStatePagerAdapter {

    protected static final String[] CONTENT = new String[] { "This", "Is", "A",
            "Test", };
    private final FragmentManager mFragmentManager;
    private static Fragment mFragmentAtTimerScreen;
    private static Fragment mFragmentAtCameraScreen;
    private static Fragment mFragmentAtRollerScreen;
    private static Fragment mFragmentAtThermoScreen;

    // Number of page
    private int mCount = ConfigManager.getInstance().NUMBER_OF_PAGE;

    public PagerAdapter(FragmentManager fm) {
        super(fm);
        mFragmentManager = fm;
        mCount = Integer.MAX_VALUE;
        // mCount = 12;
    }

    public void addData() {

    }

    /**
     * page change between tabs
     */
    PageChangeListener pageChangeListener = new PageChangeListener() {
        @Override
        public void onSwitchToNextFragment(Fragment nextFragment,
                int mPositionInAdapter) {
            Logger.error("page change listener ");
            if (mPositionInAdapter == ConfigManager.getInstance().SHUTTER_SCREEN_PAGE_ORDER) {
                mFragmentManager.beginTransaction()
                        .remove(mFragmentAtRollerScreen).commit();
                mFragmentAtRollerScreen = nextFragment;
            } else if (mPositionInAdapter == ConfigManager.getInstance().TIMER_SCREEN_PAGE_ORDER) {
                mFragmentManager.beginTransaction()
                        .remove(mFragmentAtTimerScreen).commit();
                mFragmentAtTimerScreen = nextFragment;
            } else if (mPositionInAdapter == ConfigManager.getInstance().CAMERA_SCREEN_PAGE_ORDER) {
                mFragmentManager.beginTransaction()
                        .remove(mFragmentAtCameraScreen).commit();
                mFragmentAtCameraScreen = nextFragment;
            } else if (mPositionInAdapter == ConfigManager.getInstance().THERMO_SCREEN_PAGE_ORDER) {
                mFragmentManager.beginTransaction()
                        .remove(mFragmentAtThermoScreen).commit();
                mFragmentAtThermoScreen = nextFragment;
            } else {
            }
            notifyDataSetChanged();
        }
    };

    public PageChangeListener getPageChangeListener() {
        return pageChangeListener;
    }

    private Fragment getFragment(int position) {
        Fragment f = null;
        int temp = position % 6;
        if (temp == 0) {
            f = FragmentLightManagement.newInstance("");
        }

        if (temp == 3) {
            if (mFragmentAtThermoScreen == null) {
                mFragmentAtThermoScreen = FragmentTemporatureManagement
                        .newInstance();
            }
            f = mFragmentAtThermoScreen;
        }
        if (temp == 1) {
            if (mFragmentAtRollerScreen == null) {
                mFragmentAtRollerScreen = FragmentRollerShutter.newInstance();
            }
            f = mFragmentAtRollerScreen;
        }
        if (temp == 4) {
            if (mFragmentAtTimerScreen == null) {
                mFragmentAtTimerScreen = FragmentTimerProfile.newInstance();
            }
            f = mFragmentAtTimerScreen;
        }
        if (temp == 5) {
            if (mFragmentAtCameraScreen == null) {
                mFragmentAtCameraScreen = FragmentCamera.newInstance();
            }
            f = mFragmentAtCameraScreen;
        }
        if (temp == 2) {
            f = FragmentKNXScene.newInstance();
        }

        return f;
    }

    private String makeFragmentName(int viewId, int index) {
        return "android:switcher:" + viewId + ":" + index;
    }

    @Override
    public Fragment getItem(int position) {
        Logger.error("position: " + position + " position % 6 : " + position
                % 6);
        return getFragment(position);
    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public int getItemPosition(Object object) {
        if (object instanceof FragmentTimerProfile
                && mFragmentAtTimerScreen instanceof FragmentTimerProfileDetail)
            return POSITION_NONE;
        if (object instanceof FragmentTimerProfileDetail
                && mFragmentAtTimerScreen instanceof FragmentTimerProfile)
            return POSITION_NONE;

        if (object instanceof FragmentCamera
                && mFragmentAtCameraScreen instanceof FragmentCameraZoom)
            return POSITION_NONE;
        if (object instanceof FragmentCameraZoom
                && mFragmentAtCameraScreen instanceof FragmentCamera)
            return POSITION_NONE;

        if (object instanceof FragmentRollerShutter
                && mFragmentAtRollerScreen instanceof FragmentRollerMaster)
            return POSITION_NONE;
        if (object instanceof FragmentRollerMaster
                && mFragmentAtRollerScreen instanceof FragmentRollerShutter)
            return POSITION_NONE;

        if (object instanceof FragmentTemporatureManagement
                && mFragmentAtThermoScreen instanceof FragmentTemporatureHVAC)
            return POSITION_NONE;
        if (object instanceof FragmentTemporatureHVAC
                && mFragmentAtThermoScreen instanceof FragmentTemporatureManagement)
            return POSITION_NONE;

        return POSITION_UNCHANGED;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void destroyItem(View container, int position, Object object) {
        // TODO Auto-generated method stub
        super.destroyItem(container, position, object);
        ((ViewPager) container).removeView((View) object);
    }

    public interface PageChangeListener {
        void onSwitchToNextFragment(Fragment nextFragment,
                int mPositionInAdapter);
    }

}

请告诉我这个问题。非常感谢

0 个答案:

没有答案