In one of my applications, I need to add fragments on both sides of the ViewPager. First of all, I will get a constant of 5 channels, and my ViewPager will show the feed at index 2, i.e. The current displayed Fragment will contain the data present in index 2. Thus, my entire ViewPager will show the center of 5 channels at the beginning and what I have achieved by simply setting the current ViewPager to 2, like this
viewPager.setCurrentItem(2);
Now the user can scroll both sides, when he scrolls to the left of the central position, I will search for the next feed ie from the server and add the feed at the zero index of my ViewPager, like this
feedsList.add(0, modelClassObject); // feedsList will contain all feeds and has been used by adapter to show data in fragments.
adapter.notifyDataSetChanged();
and when I scroll right from the center position, I will add the feed last, just like
feedsList.add(modelClassObject);
adapter.notifyDataSetChanged();
, i.e feedsList, , , . , , , , , . , . .
private class HorizontalPagerAdapter extends FragmentStatePagerAdapter {
public HorizontalPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
return FeedUserProfileFragment.newInstance(feedsList.get(arg0),
arg0);
}
@Override
public int getCount() {
return feedsList.size();
}
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
..: (
, , - , , , . ViewPager.