user2134412
user2134412

Reputation:

Viewpager in Android

How could i create ViewPager in both direction. Is it possible ? If it possible how could i implement it.

I have created viewpager in single direction using the below code:

private void initialisePaging() {

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, Fragment0.class.getName()));
        fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
        fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
        this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);

        ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
        pager.setAdapter(this.mPagerAdapter);
    }

Could anybody help me out..thanks!!

Upvotes: 2

Views: 419

Answers (1)

jnthnjns
jnthnjns

Reputation: 8925

Okay from what I understand, you have a list of Fragments in which you want the user to start in the middle of the Pages/Fragments, I have played with and tested the following code and it is working properly for me.

If you know the amount of Fragments and aren't dynamically changing the Fragments then this can be handled much more simply, but I am going off your code provided in the OP. Try it out:

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // This is going to initialize our Fragment list
        mSectionsPagerAdapter.init();

        // Now we are going to determine the starting position
        int startPos = mSectionsPagerAdapter.getStartPosition();

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // AFTER we set the adapter we will define our start position
        mViewPager.setCurrentItem(startPos);
    }
}

Now lets take a look at our FragmentPagerAdapter

I have added a couple methods to this class, the init() method, setCount(), and getStartPosition().

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    private int _count = 2;
    private int _start = 0;

    List<Fragment> fragments;

    public SectionsPagerAdapter(FragmentManager fm) { 
        super(fm);
    }

    public void init() {
        fragments = new ArrayList<Fragment>();
        fragments.add(new MyFragmentOne());
        fragments.add(new MyFragmentTwo());
        fragments.add(new MyFragmentThree());
        fragments.add(new MyFragmentFour());
        fragments.add(new MyFragmentFive());
        setCount();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    @Override
    public Fragment getItem(int position) {
        return (fragments.size() >= position) ? fragments.get(position) : null;
    }

    // We have created this method to get the starting position
    // You might want to play around with this to make sure the returned
    // position fits what you want for odd numbered Fragment Pages
    public int getStartPosition() {
        _start = fragments.size() / 2;
        return Math.round(_start);
    }

    public void setCount() { this._count = fragments.size(); }

    @Override
    public int getCount() { return this._count; }

    // You will need to change this to get the appropriate title per page    
    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return getString(R.string.company_select).toUpperCase(Locale.ENGLISH);
        case 1:
            return getString(R.string.companies_pending).toUpperCase(Locale.ENGLISH);
        }
        return null;
    }
}

Please Note: You did not provide examples of what you have tried so I have written code for you. StackOverflow isn't a place for this, in the future when something doesn't work give us what you have tried and we will try to help determine what went wrong.

Upvotes: 2

Related Questions