Prevent ViewPager from destroying off-screen views

前端 未结 3 2088
予麋鹿
予麋鹿 2020-12-02 04:59

I have a ViewPager hooked up to a FragmentPagerAdapter that\'s displaying three fragments. The ViewPager appears to destroy a hosted fragment\'s view when it is more than on

3条回答
  •  猫巷女王i
    2020-12-02 05:39

    In revision 4 of the Support Package, a method was added to ViewPager which allows you to specify the number of offscreen pages to use, rather than the default which is 1.

    In your case, you want to specify 2, so that when you are on the third page, the first one is not destroyed, and vice-versa.

    mViewPager = (ViewPager)findViewById(R.id.pager);
    mViewPager.setOffscreenPageLimit(2);
    

提交回复
热议问题