更改要在ViewPagerIndicator中显示的初始页面

时间:2012-03-21 11:22:09

标签: android android-viewpager

我已经使用ActionBarSherlock实现了ViewPagerIndicator。

现在,我想在创建viewpager后在viewpager中打开一个特定的片段。通常你应该使用setCurrentItem(pageid)或setViewPager(mPager,pageid);

但这不起作用:(如果我想在viewpager中打开片段2,那么 viewpager指示器显示片段2显示,但实际上片段1(标准)显示在viewpager中。

我有以下代码:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_tabs);

        mAdapter = new ColofonFragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
        mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
        String page = "";
        page = getIntent().getStringExtra("page");
        int pageid = 0;
        if(page.equals("contact")) { pageid = 0; }
        else if(page.equals("colofon")) { pageid = 1; }
        else if(page.equals("disclaimer")) { pageid = 2; }      

        mIndicator.setViewPager(mPager, pageid); // Doesn't work...
            //mIndicator.setCurrentItem(pageid); // Doesn't work...
    }

class ColofonFragmentAdapter extends FragmentPagerAdapter implements TitleProvider {

        ArrayList<Fragment> fragments = new ArrayList<Fragment>();
        ArrayList<String> titels = new ArrayList<String>();

        public ColofonFragmentAdapter(FragmentManager fm) {
            super(fm);
            fragments.add(new LayoutFragment(R.layout.colofon_contactgegevens));
            titels.add("Contact");
            fragments.add(new LayoutFragment(R.layout.colofon_colofon));
            titels.add("Colofon");
            fragments.add(new LayoutFragment(R.layout.colofon_disclaimer));
            titels.add("Disclaimer");
        }


        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

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

        @Override
        public String getTitle(int position) {
            return titels.get(position);
        }
    }

您能告诉我应该使用哪种方法来更改初始页面吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这是由setCurrentItem()引起的错误 https://github.com/JakeWharton/Android-ViewPagerIndicator/pull/76

它已经解决了^^