简体   繁体   中英

Android ViewPager: Destroy Fragment on Slide?

I am basically playing an animation on each fragment of the view pager. The animation plays when the user slides to the specific fragment. However, certain fragments don't play the animation the second time I visit them. That's because the view pager keeps them in memory.

I need to destroy each fragment after the user slides to another fragment. This way, the animations play every time I revisit those fragments.

Main View:

pager = (ViewPager) findViewById(R.id.guidepager);
mAdapter = new NewUserGuideAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
pager.setOffscreenPageLimit(0); //Tried this too. Didnt work

Fragment:

public class NewUserPage_Two extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_new_user_page__two, container, false);

        //Play animation, etc
       Animation animation_1 = AnimationUtils.loadAnimation(NewUserPage_Two.this.getActivity(), R.anim.abc_slide_in_bottom);
        person1.setAnimation(animation_1);

      return rootView;
}

Adapter:

public class NewUserGuideAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {

        switch (index) {
            case 0:
                return new NewUserPage_One();
            case 1:
                return new NewUserPage_Two();
            case 2:
                return new NewUserPage_Three();
            case 3:
                return new NewUserPage_One();
            case 4:
                return new NewUserPage_One();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 5;
    }
}

How can I amend my code guys?

Try this inside the fragment:

((BaseAdapter) *YourContainer*.getAdapter()).notifyDataSetChanged();


You can refer to: Refresh Current Fragment (ListView Data) remaining in the same activity

ViewPager提供了一种方法mViewPager.setOffscreenPageLimit(0);

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

public void setOffscreenPageLimit(int limit) {
    if (limit < DEFAULT_OFFSCREEN_PAGES) {
        Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to " +
                DEFAULT_OFFSCREEN_PAGES);
        limit = DEFAULT_OFFSCREEN_PAGES;
    }
    if (limit != mOffscreenPageLimit) {
        mOffscreenPageLimit = limit;
        populate();
    }
}

That's the method body, DEFAULT_OFFSCREEN_PAGES=1 by the way.I think google add this limits cause you need at least the 2 views between current item while you are sliding. You can try using addOnPageChangeListener() and start your animation on onPageSelected .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM