2

我怎么能ViewPagerboth direction. 是否可以 ?如果可能的话,我该如何实现它。

viewpager使用以下代码在单向创建:

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);
    }

谁能帮帮我..谢谢!!

4

1 回答 1

2

好吧,据我了解,您有一个列表Fragments,您希望用户在Pages/中间开始Fragments,我已经使用并测试了以下代码,它对我来说可以正常工作。

如果您知道数量Fragments并且没有动态更改,Fragments那么这可以更简单地处理,但我将关闭您在 OP 中提供的代码。试试看:

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);
    }
}

现在让我们看看我们的FragmentPagerAdapter

我在这个类中添加了几个方法,init()方法setCount()、 和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;
    }
}

请注意:您没有提供您尝试过的示例,因此我为您编写了代码。StackOverflow 不适合这样做,将来当某些事情不起作用时,请给我们您尝试过的内容,我们将尝试帮助确定出了什么问题。

于 2013-04-08T14:34:42.990 回答