Android Viewpager show wrong pages

我与影子孤独终老i 提交于 2019-12-10 23:44:36

问题


I have a viewpager and in that viewpager i have 6 pages.All of the pages contains listviews.Sometimes viewpager shows wrong pages at wrong indexes.Here is my viewpager adapter:

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

}

Here is how i create my 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);
    }

}

Thanks for your help


回答1:


try this:

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



回答2:


this mIndicator.setCurrentItem(1); must replace by this mPager.setCurrentItem(1);



来源:https://stackoverflow.com/questions/14046889/android-viewpager-show-wrong-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!