Reputation: 2876
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) {
}
});
Upvotes: 0
Views: 1595
Reputation: 823
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)
Upvotes: 0
Reputation: 300
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.
Upvotes: 2