2

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!!

  • @AbhishekSabbarwal thanks for your answer let me check the link –  Apr 08 '13 at 08:20
  • @AbhishekSabbarwal i want to make viewpager in both direction not in single direction!! –  Apr 08 '13 at 08:25
  • you can do it using this library and set pager orientation horizontal and vertical [here](http://stackoverflow.com/questions/10720276/error-including-android-directionalviewpager-jar-in-eclipse/14268702#14268702) – sheetal Apr 08 '13 at 08:14
  • @sheetal thanks for your answer let me check !! –  Apr 08 '13 at 08:20
  • sheetal i did not get any library there!! –  Apr 08 '13 at 08:23
  • click on the link answeres by MrMaffen...and your jar will be downloaded – sheetal Apr 08 '13 at 08:25
  • sheetal i want to make viewpager in both direction not in single direction!! –  Apr 08 '13 at 08:26
  • yeah it will show in both horizontal and vertical direction...by default viewpager is horizontal..i have used this library – sheetal Apr 08 '13 at 08:27
  • when you will define your viewpager set your viewpager.setOrientation horizontal or vertical at run time – sheetal Apr 08 '13 at 08:30
  • @sheetal i dont want it horizontal and vertical i want it should slide in both direction left and as well as right!! –  Apr 08 '13 at 08:48
  • @priya2134412 yeas horizontal and vertical means the same..it will slide....i m not talking about orientation...why dont you try and speak! OR better go for a R&D – sheetal Apr 08 '13 at 08:51
  • @priya2134412 first try before you write another comment – sheetal Apr 08 '13 at 08:52
  • @sheetal i have tried but i didn't get could you help me out!! –  Apr 08 '13 at 11:03
  • @priya2134412 When I read this, and I see that you want it to slide "left to right and right to left", I am not sure I fully understand. Let's say your `List` contains 8 `Fragments`, do you want it to start in the middle, at 4, and then be able to scroll left or right from there? – jnthnjns Apr 08 '13 at 12:45
  • @Asok thanks for your response and yes..right ..i want to do like that only!! –  Apr 08 '13 at 13:11

1 Answers1

2

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.

jnthnjns
  • 8,962
  • 4
  • 42
  • 65