1

If we have 4 pages in a View pager and swipe from positions 0 do 3 Item at position 0 is destroyed when we get to position 2, item at position 1 is destroyed when we get to position 4.

I need them to be destroyed right away, because I want the view to be recreated if I go back to it, which doesnt happen at the moment.

public class ViewPagerTutorialAdapter extends FragmentStatePagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        Bundle args = new Bundle();
        args.putInt("page", position);
        TutorialPageFragment fragment = new TutorialPageFragment();
        fragment.setArguments(args);
        return fragment;
    }

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

public class TutorialPageFragment extends android.support.v4.app.Fragment {

    private ImageView ivTutorialPage;
    private TransitionDrawable myTransitionDrawable;
    private View rootView;

    public TutorialPageFragment() {
    }

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

    @Override
    public void onResume() {
        super.onResume();
        ivTutorialPage = (ImageView) rootView.findViewById(R.id.ivTutorialPage);
        ivTutorialPage.setImageResource(R.drawable.fadein);
        myTransitionDrawable = (TransitionDrawable) ivTutorialPage.getDrawable();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        myTransitionDrawable.startTransition(1000);
                    }
                });
            }
        }, 1000);
    }
}
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • `because I want the view to be recreated if I go back to it` - why? Creating views is expensive and you'll likely experience stutter. There is probably a better way to do whatever it is you want to do other than recreate all views. – poss Sep 12 '15 at 13:50
  • every page has a background - image_1. Whenever user swipes and focuses on the page, I want to smoothly transition to image_1a. The only way for this to happen is recreation of the view. Otherwise if I go 0, 1, 2 – Kaloyan Roussev Sep 12 '15 at 13:54
  • if I go 0 1 2 and then go back to 1, I can see the already transitioned state to image_1a, instead of see the animation again – Kaloyan Roussev Sep 12 '15 at 13:57
  • how about overriding `setUserVisibleHint` in fragment to run animation when fragment becomes visible? – poss Sep 12 '15 at 14:02
  • ViewPager needs at least to create previous and next page views in advance so you don't see the view appearing after you start sliding. Instead, you should reset your animations after the current page changes. – BladeCoder Sep 12 '15 at 14:21
  • @poss setUserVisibleHint - this is taking me into the right direction! – Kaloyan Roussev Sep 12 '15 at 14:23
  • @poss I can hear Europe - The final countdown victoriously playing in my head, because setUserVisibleHint solved all my problems and now life is good again! If you post this as the answer, I'd accept it – Kaloyan Roussev Sep 12 '15 at 14:30
  • Haha, I'm glad it helped you :) – poss Sep 12 '15 at 21:58

2 Answers2

1

Try viewPager.setOffscreenPageLimit(1);. This should save only one page near selected one. There is not way to only load selected item at a time according to this.

Jemshit
  • 9,501
  • 5
  • 69
  • 106
  • so how do I make the fragments recreate whenever focused on? I tried countless things using onPageChangedListener, but nothing works out. – Kaloyan Roussev Sep 12 '15 at 13:58
  • I am not sure how to recreate fragment everytime tab is selected. You can still update your data when fragment becomes visible to user instead of recreating whole fragment: http://stackoverflow.com/a/29621490/3736955 – Jemshit Sep 12 '15 at 14:06
1

While this is not exactly answer to asked question, a better practice is not recreate Views if not necessary because it can be expensive to do on more complicated layouts + cause stuttering / extra lag which makes user experience worse.

In this case (running animations when fragment becomes visible) it's better to override setUserVisibleHint in fragment with code to run animation which will trigger when user visits the screen. The main advantage is of course no need to recreate Views and allows Android to perform it's internal optimization naturally.

poss
  • 1,759
  • 1
  • 12
  • 27