I know there are a few questions on infinite paging, but I am still having difficulty in getting any of them to work. (I am working in Android Studio)
I am trying to get the InfiniteViewPager from https://github.com/antonyt/InfiniteViewPager up and running.
I am trying to get this working so that I am able to swipe across 4 pages/fragments (A, B, C, D), with functionality for the right swipe on D to return to A, and a left swipe on A to return to D.
I've imported the InfinitePagerAdapter and InfiniteViewPager Java classes, and added the InfiniteViewPager to my activity_main.xml
file.
However, I'm stuck with where I need to "Wrap your existing PagerAdapter with the InfinitePagerAdapter".
Here is my code:
public class MainActivity extends AppCompatActivity {
PagerAdapter mPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mPagerAdapter);
//Cannot resolve symbol adapter
PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter);
mViewPager.setAdapter(wrappedAdapter);
mViewPager.setOffscreenPageLimit(5);
}
public class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//getItem is called to instantiate the fragment for the given page.
switch (position) {
default:
return Fragment1.newInstance();
case 1:
return Fragment2.newInstance();
case 2:
return Fragment3.newInstance();
case 3:
return Fragment4.newInstance();
}
}
@Override
public int getCount() {
// Show number of total pages.
return 4;
}
}
}
I'm not exactly sure what I should be replacing adapter
with, or if it should be pulled from the other Java Classes. Additionally is there anything else I need to add/change to get this working?
I've got it working after a bit of tinkering with the demo - code is below:
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return 4;
}
@Override
public Fragment getItem(int position) {
final ArrayList<Fragment> fragmentPosition = new ArrayList<>();
fragmentPosition.add(new Fragment1());
fragmentPosition.add(new Fragment2());
fragmentPosition.add(new Fragment3());
fragmentPosition.add(new Fragment4());
Fragment fragment = fragmentPosition.get(position);
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
};
// wrap pager to provide infinite paging with wrap-around
PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter);
// actually an InfiniteViewPager
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(wrappedAdapter);
}
}