2

我有一个 viewpager,在那个 viewpager 中我有 6 页。所有页面都包含 listviews。有时 viewpager 在错误的索引处显示错误的页面。这是我的 viewpager 适配器:

public class TestFragmentAdapter extends FragmentPagerAdapter{
    protected static final String[] CONTENT = new String[] { "a","b","c","d","e","f" };

    private int mCount = CONTENT.length;
    Fragment fragment1,fragment2,fragment3,fragment4,fragment5,fragment6;

    public TestFragmentAdapter(FragmentManager fm) {
        super(fm);
        fragment1 = new CategoryListView();
        fragment2 = CustomizedListviewRecent.newInstance(41);
        fragment3 = CustomizedListviewMostRecents.newInstance(0);
        fragment4 = CustomizedListviewPopulars.newInstance();
        fragment5 = CustomizedListviewRecent.newInstance(42);
        fragment6 = CustomizedListviewRecent.newInstance(43);

    }

    @Override
    public Fragment getItem(int position) {
        if(position == 0)
            return fragment1;
        else if(position == 1)
            return fragment2;
        else if(position == 2)
            return fragment3;
        else if(position == 3)
            return fragment4;
        else if(position == 4)
            return fragment5;
        else
            return fragment6;
    }
    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public CharSequence getPageTitle(int position) {
      return TestFragmentAdapter.CONTENT[position % CONTENT.length];
    }

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    }

}

这是我创建viewpager的方式:

public class SampleTitlesDefault extends BaseSampleActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
        mIndicator.setCurrentItem(1);
    }

}

谢谢你的帮助

4

2 回答 2

0

尝试这个:

public class TestFragmentAdapter extends FragmentPagerAdapter{
    protected static final String[] CONTENT = new String[] { "a","b","c","d","e","f" };
    ArrayList<Fragment> fragments;

    public TestFragmentAdapter(FragmentManager fm) {
        super(fm);
        fragments.add(new CategoryListView());
        fragments.add(CustomizedListviewRecent.newInstance(41));
        fragments.add(CustomizedListviewMostRecents.newInstance(0));
        fragments.add(CustomizedListviewPopulars.newInstance());
        fragments.add(CustomizedListviewRecent.newInstance(42));
        fragments.add(CustomizedListviewRecent.newInstance(43));
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
      return TestFragmentAdapter.CONTENT[position % CONTENT.length];
    }
}
于 2012-12-26T23:00:31.337 回答
-1

mIndicator.setCurrentItem(1); 必须用这个代替mPager.setCurrentItem(1);

于 2013-06-21T02:39:49.003 回答