ViewPager片段被调用两次

时间:2014-05-19 21:01:18

标签: android android-fragments android-viewpager

好的,所以我正在尝试实现一个包含三个页面的ViewPager,并在每个页面上都有独特的内容,所以我建立了一个系统,我根据页面索引来扩展不同的布局。这一切在我导航时起作用,但我注意到Fragment被调用了两次。

这是我的FragmentAcivity类:

public class TutorialScreen extends FragmentActivity {

    private static final String TAG = "TAG";
    private static final int NUM_PAGES = 3;
    private ViewPager pager; // animation handler
    private PagerAdapter adapter; // provides pages to pager

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tutorial_layout);

        // Instantiate Pager & Adapter
        pager = (ViewPager) findViewById(R.id.pager);
        adapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
        Log.v(TAG, "Initial Page is " + pager.getCurrentItem()); // this logs '0' - which is what i want
        pager.setCurrentItem(pager.getCurrentItem());
    }

    @Override
    public void onBackPressed() {
        if(pager.getCurrentItem() == 0) {
            super.onBackPressed();
        }
        else {
            pager.setCurrentItem(pager.getCurrentItem() -1);
        }
    }

    // Pager Adapter SubClass
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
            Log.v(TAG, "constructor"); // this logs once
            pager.setCurrentItem(pager.getCurrentItem());
        }

        @Override
        public Fragment getItem(int position) {
            Log.v(TAG, "getItem " + position); // this logs twice
            return new ScreenSlidePageFragment(position); // note I've overridden the constructor
        }

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

和我的片段类:

public class ScreenSlidePageFragment extends Fragment {

    private static int pageIndex;
    private static final String TAG = "TAG";

    ScreenSlidePageFragment(int id) {
        pageIndex = id;
        Log.v(TAG, "Page Index is " + pageIndex); // this logs twice
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        int resID = -1;

        switch(pageIndex) {
            case 0:
                resID = R.layout.tutorial_page1;
                break;
            case 1:
                resID = R.layout.tutorial_page2;
                break;
            case 2:
                resID = R.layout.tutorial_page3;
                break;
            default:
                resID = R.layout.tutorial_page1;
                break;
        }

        Log.v(TAG, "Res Index is " + resID); // this logs twice
        ViewGroup rootView = (ViewGroup) inflater.inflate(resID, container, false);
        return rootView;
    }
}

日志

构造 getItem 0 页面索引为0 getItem 1 页面索引是1 Res Index是2130903043 Res Index是2130903043

所以基本上它会立即跳到第二页,无论我是否用setCurrentItem()设置位置......

我认为我可能会因为尝试只有一个Fragment类而不是每个匹配布局的单独一个而作弊,但是......这应该有用吗?

请注意,我看到一个(我认为)相关的问题,第二个(初始)页面出现两次 - 一次最初,然后再次刷到下一页(应该是第3页?)。一旦我导航到第3页(经过两次滑动)后,我可以一直回到第1页(应该是初始页面)。

思考?我做错了什么?

2 个答案:

答案 0 :(得分:3)

所以,我解决了我自己的(根)问题(虽然不是我遇到的奇怪的索引案例)。

我对ViewPager中每个页面的单个内容实例化的误解是,会反复使用一个Fragment,我会将参数传递给它的构造函数,以便告诉它哪个布局为膨胀。

相反,我意识到我应该拥有完全独立的Fragment类,每个类都拥有它自己独特的布局,我可以使用FragmentStatePagerAdapter中getItem()的相同位置id来实例化正确的Fragment ......现在对我有意义。

见下面的代码:

public class TutorialScreen extends FragmentActivity {

    private static final int NUM_PAGES = 3;
    private ViewPager pager; 
    private PagerAdapter adapter; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tutorial_layout);

        // Instantiate Pager & Adapter
        pager = (ViewPager) findViewById(R.id.pager);
        adapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {
        if(pager.getCurrentItem() == 0) {
            super.onBackPressed();
        }
        else {
            pager.setCurrentItem(pager.getCurrentItem() -1);
        }
    }

    // PagerAdapter SubClass
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment;
            switch(position) {
                case 0:
                    fragment = new TutorialPage1();
                break;
                case 1:
                    fragment = new TutorialPage2();
                break;
                case 2:
                    fragment = new TutorialPage3();
                break;
                default:
                    fragment = new TutorialPage1();
                break;
            }
            return fragment;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

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

    } /* EOC */

} /* EOC */ 

答案 1 :(得分:0)

所以,我正在使用相同的片段。 适配器:

public class IncomeWeekFragmentAdapter extends FragmentStatePagerAdapter {

    List<String> titlesList;
    List<String> reqDates;

    public IncomeWeekFragmentAdapter(FragmentManager fm, List<String> titles, List<String> reqDates) {
        super(fm);
        this.titlesList = titles;
        this.reqDates = reqDates;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        Bundle args = new Bundle();
        if(fragment==null)
        fragment = new IncomeWeekFragment();
        if (position == 0) {
            args.putString("date_start", reqDates.get(1));
            args.putString("date_end", reqDates.get(0));
        } else if (position == 1) {
            args.putString("date_start", reqDates.get(3));
            args.putString("date_end", reqDates.get(2));
        } else if (position == 2) {
            args.putString("date_start", reqDates.get(5));
            args.putString("date_end", reqDates.get(4));
        } else if (position == 3) {
            args.putString("date_start", reqDates.get(7));
            args.putString("date_end", reqDates.get(6));
        }
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titlesList.get(position);
    }
}