Search code examples
androidfragmentpageradapter

Recycling FragmentPagerAdapter


I'm building a FragmentPagerAdapter (The one with the small icons) which I want;

  • To the first item in the middle (Horizontal) of the screen
  • To swipe through it and make them go one 5th of the screen to the left/right

What would be the best way to achieve this?

This is a visual representation:

This is the code of the FragmentPagerAdapter:

public class NavigationPagerAdapter extends FragmentPagerAdapter {
    public NavigationPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return NavigationFragment.newInstance(position);
    }

    @Override
    public int getCount() {
        return stores.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return null;
    }

    @Override
    public float getPageWidth(int position) {
        return 0.2f;
    }
}

Solution

  • I hope I understood this right. Try to add the following configuration to your `ViewPager.

    pager.setOffScreenPageLimit(4);
    pager.setClipToPadding(false);
    pager.setPageMargin(-500);
    pager.setHorizontalFadingEdgeEnabled(true);
    pager.setFadingEdgeLength(20);
    

    Feel free to customize the values and get your desired effect