我有一个jake Wharton的viewpagerindicator,在那个viewpager我有6个页面。所有的页面都包含listviews,我用AsyncTask加载它们。有时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);
}
}
这里有什么问题?我该如何解决?谢谢你的帮助
答案 0 :(得分:2)
我知道它太晚了但可能有人需要它。
我也面临着关于错误位置的类似问题,并且由于错误的位置,它在两个片段上显示相同的结果。我通过用FragmentPagerAdapter
FragmentStatePagerAdapter
来解决我的问题
我还使用onPageScrolled
inteface
OnPageChangeListener
方法传递了索引位置
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) {
}
});
就像文档说的那样,以这种方式思考。如果您要像书阅读器那样执行应用程序,则不希望一次将所有片段加载到内存中。您希望在用户阅读时加载和销毁Fragments。在这种情况下,您将使用FragmentStatePagerAdapter。如果您只是显示3&#34;标签&#34;它不包含大量繁重的数据(如Bitmaps),那么FragmentPagerAdapter可能很适合你。另外,请记住,ViewPager默认会将3个片段加载到内存中。您提到的第一个适配器可能会破坏View层次结构并在需要时重新加载它,第二个适配器只保存片段的状态并完全销毁它,如果用户然后返回到该页面,则会检索状态。
Difference between FragmentPagerAdapter and FragmentStatePagerAdapter
答案 1 :(得分:0)
请不要自己“缓存”Fragment
,因为它已经为你做了。
也就是说,在newInstance
方法中你应该getItem
,在创建适配器时你应该不。例如:
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);
}
这样做的常见原因是: