Viewpager shows wrong page

限于喜欢 提交于 2019-12-13 13:02:00

问题


I have a viewpagerindicator by Jake Wharton and in that viewpager i have 6 pages.All of the pages contains listviews and i'm loading them with an AsyncTask.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);
    }

}

What can be problem here?How can i solve this?Thanks for your help


回答1:


I know its too late but may be some one need it.

I have also faced the similar problem regarding wrong position and some time it shows same results on two fragments due to wrong positon.I solved my problem by replacing the FragmentPagerAdapter with FragmentStatePagerAdapter

And I also passed index postion from onPageScrolled method using OnPageChangeListener inteface

 viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


        }

        @Override
        public void onPageSelected(int position) {

            bundle = new Bundle();
            bundle.putInt("position", position);
            primaryFragment.setCurrentPostionAndData(position);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

Like the docs say, think about it this way. If you were to do an application like a book reader, you will not want to load all the fragments into memory at once. You would like to load and destroy Fragments as the user reads. In this case you will use FragmentStatePagerAdapter. If you are just displaying 3 "tabs" that do not contain a lot of heavy data (like Bitmaps), then FragmentPagerAdapter might suit you well. Also, keep in mind that ViewPager by default will load 3 fragments into memory. The first Adapter you mention might destroy View hierarchy and re load it when needed, the second Adapter only saves the state of the Fragment and completely destroys it, if the user then comes back to that page, the state is retrieved.

Difference between FragmentPagerAdapter and FragmentStatePagerAdapter




回答2:


Please don't "cache" the Fragments yourself because it already does that for you.

That said, you should newInstance them inside the getItem method, and not when creating the adapter. Example:

public TestFragmentAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    if(position == 0)
        return new CategoryListView();
    else if(position == 1)
        return CustomizedListviewRecent.newInstance(41);
    else if(position == 2)
        return CustomizedListviewMostRecents.newInstance(0);
    else if(position == 3)
        return CustomizedListviewPopulars.newInstance();
    else if(position == 4)
        return CustomizedListviewRecent.newInstance(42);
    else
        return CustomizedListviewRecent.newInstance(43);
}

Common reasons to do that are:

  1. People think it's more efficient to do it that way; or
  2. A reference to the Fragment may be needed later on.

For (1), as I said, you don't need to worry about it (unless you want to learn it and tweak later, but for now let it be). And if you need (2), then you can do this (originally seen here).



来源:https://stackoverflow.com/questions/14540320/viewpager-shows-wrong-page

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