其他分享
首页 > 其他分享> > android-ViewPager片段被调用两次

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;
        }   
    } 
}

和我的Fragment类:

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索引是2130903043
RES索引是2130903043

因此,基本上不管我是否使用setCurrentItem()设置位置,它都会立即跳至第二页.

我认为我可能通过尝试只具有一个Fragment类而不是为每个匹配的布局分配单独的类而作弊,但是…这应该起作用吗?

请注意,我看到一个(我认为)相关的问题,第二个(初始)页面显示了两次-最初显示一次,然后再次滑动到下一页(应该是第3页?).导航到第3页(两次滑动之后)后,我可以一直回到第1页(应该是初始页面).

有什么想法吗?我究竟做错了什么 ?

解决方法:

因此,我解决了我自己的(根本)问题(尽管不是我遇到的怪异的索引情况).

我对在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 */ 

标签:android-fragments,android-viewpager,android
来源: https://codeday.me/bug/20191121/2053901.html