android-fragments ViewPager片段被调用两次

fivyi3re  于 2022-11-13  发布在  Android
关注(0)|答案(3)|浏览(226)

好的,我尝试实现一个有三个页面的ViewPager,并且在每个页面上都有唯一的内容,所以我建立了一个系统,在这个系统中,我根据页面索引膨胀不同的布局。这在我导航时就可以工作了,但是我注意到Fragment被调用了两次。
下面是我的FragmentActivity类:

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资源索引是2130903043资源索引是2130903043
所以基本上它是马上跳到第二页,不管我是否用setCurrentItem()设置位置...
我想我可能是作弊,试图只有一个片段类,而不是一个单独的一个为每个匹配的布局,但...这应该工作?
请注意,我看到了一个(我认为)相关问题,即第二(初始)页面显示两次-一次是初始页面,然后再次滑动到下一页(应该是第3页?)。一旦我导航到第3页(两次滑动后),我就可以一直返回到第1页(应该是初始页面)。
想法?我做错了什么?

ljo96ir5

ljo96ir51#

所以,我解决了我自己的(根)问题(尽管不是我所经历的奇怪的索引情况)。
我对ViewPager中每个页面的单个内容的示例化的误解是,会有一个单独的片段被反复使用,我会将参数传递给它的构造函数,以便告诉它要膨胀哪个布局。
相反,我意识到我应该拥有完全独立的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 */
q5iwbnjs

q5iwbnjs2#

所以我用的是同样的片段

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

yfwxisqw3#

设置屏幕外页面限制(1);随片段数变化1

相关问题