Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager scroll half screen

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: Half screen scroll ViewPager

like image 730
Nifhel Avatar asked Aug 19 '13 09:08

Nifhel


1 Answers

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);
} 
like image 132
Nifhel Avatar answered Sep 28 '22 03:09

Nifhel