gpt4 book ai didi

Android Viewpager 显示错误的页面

转载 作者:行者123 更新时间:2023-11-29 14:15:52 24 4
gpt4 key购买 nike

我有一个 viewpager,在那个 viewpager 中我有 6 个页面。所有页面都包含 ListView 。有时 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);
}

}

谢谢你的帮助

最佳答案

试试这个:

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

关于Android Viewpager 显示错误的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14046889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com