I have a list of Fragments that I have to put in a ViewPager (no problem so far).
ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
fragmentList.add(new fragment1());
fragmentList.add(new fragment2());
fragmentList.add(new fragment3());
fragmentList.add(new fragment4());
FragmentAdapter mAdapter = new FragmentAdapter( mFragmentManager, fragmentList);
mPager = (ViewPager) view.findViewById(R.id.pager_items_list);
mPager.setCurrentItem(0);
mPager.setAdapter(mAdapter);
mPager.setOnPageChangeListener(pageChangeListener);
The adapter
public class FragmentAdapter extends FragmentStatePagerAdapter {
ArrayList<Fragment> fragmentsList;
public FragmentAdapter(FragmentManager fm, ArrayList<Fragment> fragmentsList) {
super(fm);
this.fragmentsList = fragmentsList;
}
@Override
public int getCount() {
return fragmentsList.size();
}
@Override
public Fragment getItem(int position) {
if (position<fragmentsList.size() & fragmentsList.get(position)!=null) {
return fragmentsList.get(position);
}
return null;
}
}
What I need is to make the ViewPager to show two items on the screen, but scroll only by one every time.
This image may help me explain what I mean:
Thanks to TeeTracker's comment I found the solution for your problem. I just had to Override getPageWidth in my Adapter:
@Override
public float getPageWidth(int position) {
return(0.5f);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With