0

I've been following this tutorial: https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html on how to use fragmentstatepageradapters. It works well but I can't seem to be able to use the ArrayListFragment's instance number. In the tutorial there is this bit displaying the instance number as a header:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("Fragment #" + mNum);
...

When I start the app the first screen show Fragment 0. when I swipe to the next screen it shows Fragment 1 and so forth. But when I add this:

Log.i("instancenumber", Integer.toString(mNum));

right below ((TextView)tv)... the log shows

instancenumber 0

instancenumber 1

right when I start the app before I even touched anything and when I swipe right it switches to 2 and when I swipe to the last one it doesn't log anything and going backwards somehow also doesn't update correctly.

I'm guessing I'm not understanding either the concept of instance numbers or the onCreateView method correctly. How can I get the correct instance number? It seems to work with the textview.

thanks in advance,

hachel

PS: context: I need the number because I have an mp3 playing and I want it to play faster (playback speed calculated by increasing instance number) and I also display a new image when I swipe to the next screen

hachel
  • 45
  • 3

2 Answers2

0

FragmentStatePagerAdapter always create the current view and the next one, so it will call the onCreateView from to the first and the next even when just showing the first one.

It does this to improve performance, when you scroll to the next view, the view is already created and Android can show it right way. When you scroll to the second view, the onCreateView of the third view will be called.

You can set how much fragments the PagerAdapter will keep in memory with setOffscreenPageLimit, there are more info in this question: ViewPager FragmentStatePagerAdapter keep more than three fragments in memory

Community
  • 1
  • 1
jonathanrz
  • 4,206
  • 6
  • 35
  • 58
  • ok, I could disable/set to zero caching of fragments, i'll look into that. Is there another way to dynamically see on which fragment I currently am? – hachel Oct 08 '16 at 16:06
  • @hachel You should not disable it, it will create a lag when you change the selected fragment. To get the current fragment, you can use this: http://stackoverflow.com/questions/18609261/getting-the-current-fragment-instance-in-the-viewpager – jonathanrz Oct 08 '16 at 16:19
  • ok thanks, I guess I can't be using oncreateview for that then because of the caching but rather use some listener for that? – hachel Oct 09 '16 at 12:00
  • I made it work like below. I now have a new problem with my mediaplayer in combination with currentItem but I'll post that in another question. thank you very much – hachel Oct 09 '16 at 14:13
0

with the help of jonathanrz I managed to make it work like this:

mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageScrollStateChanged(int state) {}
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

            public void onPageSelected(int position) {
                Log.i("int", Integer.toString(mPager.getCurrentItem()));
            }
        });
hachel
  • 45
  • 3
  • You just needed the index of the position? I thought that you need some more custom data. – jonathanrz Oct 09 '16 at 14:18
  • this is what I'm trying to do: http://stackoverflow.com/questions/39944680/play-different-looping-sound-each-time-i-swipe-to-a-new-fragment – hachel Oct 09 '16 at 14:31