0

I want to use ViewPager to swipe through songs in my Media Player ( going forward or backward like SoundCloud app for android ). I have created a fragment which I inflate and set inside information like song name, author etc.

I have read some topics on StackOverflow about the ViewPager displays wrong items ( Viewpager shows wrong page and Android Viewpager show wrong pages and a lot more ) but I don't have fixed number of XML's to use with the ViewPager.

My Data is displayed in a RecyclerView, when I am pressing the item on index 4 for example I call the following method:

       mPager.setCurrentItem(position);

So the mPager is set to position 4.

    @Override
    public Fragment getItem(int position) {
        Log.i("TEST","Returnin a fragment on position "+position);
        return fragment = new MusicSliderFragment();
    }

But on my Adapter when it's changing I get the following output:

02-23 10:39:57.131 7599-7599/************** I/TEST: Returning a fragment on position 4
02-23 10:39:57.131 7599-7599/************** I/TEST: Returning a fragment on position 3
02-23 10:39:57.131 7599-7599/************** I/TEST: Returning a fragment on position 5

And when my application is launched I get the following output:

I/TEST: Returning a fragment on position 0
I/TEST: Returning a fragment on position 1

The main problem is when I am opening the app first time and set the song on position 4 the fragment information are displayed in the next fragment.

Is there a way how can I fix this?

Adapter Class:

 private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

        private MusicSliderFragment fragment;

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

        @Override
        public Fragment getItem(int position) {
            Log.i("TEST","Returnin a fragment on position "+position);
            return fragment = new MusicSliderFragment();
        }

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

        public void updateSongName(String name) {
            fragment.setSongName(name);
        }
    }

The RecyclerView Listener :

            musicList.addOnItemTouchListener(
            new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, final int position) {

                        Log.i("TEST", "Setting the mPager position to: "+position);
                        mPager.setCurrentItem(position);
                    }
                }


And the `Listener` of the `mPager` where I update the song name for example:

mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                play(position);

                ScreenSlidePagerAdapter adapter = (ScreenSlidePagerAdapter) mPager.getAdapter();
                adapter.updateSongName(splitName(myDataList.get(position))[0]);

                Log.i("TEST", "Playing the position: "+position);
                mPagerAdapter.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
Community
  • 1
  • 1
Marian Pavel
  • 2,726
  • 8
  • 29
  • 65

2 Answers2

2

View pager can load neighbour fragments when you choose current. Method setOffscreenPageLimit() defines how many neighbours you want to preload. onPageSelected(int position) should work correct and returns current item.

  • So the viewpager load the page 4 and then load the neighbour ( 3 and 5 ), how can I make it return the fragment 4 then :) ? – Marian Pavel Feb 23 '16 at 09:17
  • if you want to reach fragment 4 you can swipe one by one. Or if you whant reach it immediately you can pager.setCurrentItem( num ). – Konstantin Volkov Feb 23 '16 at 09:25
  • I am doing that in RecyclerView listner, but then how you said, it's getting it's neighbour 3 and 5 and the current fragment remains 5. – Marian Pavel Feb 23 '16 at 09:34
0

Viewpager indexing starts from zero if you want to load the fourth Fragment then you need to set the position 3.

  mPager.setCurrentItem(3); //Load the 4th fragment according to you.(0-3)
Murali krishna
  • 823
  • 1
  • 8
  • 23
  • My recyclerview starts from 0 too, so when I am loading the index 4 the mPager set the index to 4, I don't think this is the problem, if I do the position - 1 trick when I am pressing the index 0 from RecyclerView it will call the setCurrentItem to -1 and that is not correct. – Marian Pavel Feb 23 '16 at 09:49