在某些设备上,我看到片段顺序是1,2,3,4 和其他是2,3,4,1
如何解决?
代码
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment1(), "1"); // 1
adapter.addFragment(new Fragment2(), "2"); // 2
adapter.addFragment(new Fragment3(), "3"); // 3
adapter.addFragment(new Fragment4(), "4"); // 3
viewPager.setAdapter(adapter);
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> _fragmentList = new ArrayList<>();
private final List<String> _fragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return _fragmentList.get(position);
}
@Override
public int getCount() {
return _fragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
_fragmentList.add(fragment);
_fragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return _fragmentTitleList.get(position);
}
}
答案 0 :(得分:0)
试试这个。
<强> ViewPagerAdapter 强>
public class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> _fragmentList = new ArrayList<>();
private List<String> _fragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager, List<Fragment> _fragmentList, List<String> _fragmentTitleList) {
super(manager);
this._fragmentList = _fragmentList;
this._fragmentTitleList = _fragmentTitleList;
}
@Override
public Fragment getItem(int position) {
return _fragmentList.get(position);
}
@Override
public int getCount() {
return _fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return _fragmentTitleList.get(position);
}
}
并使用它。
List<Fragment> fragmentList = new ArrayList<>();
List<String> fragmentTitleList = new ArrayList<>();
// add fragment
fragmentList.add(new Fragment1());
fragmentList.add(new Fragment2());
fragmentList.add(new Fragment3());
fragmentList.add(new Fragment4());
// add title
fragmentTitleList.add("1");
fragmentTitleList.add("2");
fragmentTitleList.add("3");
fragmentTitleList.add("4");
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList, fragmentTitleList);
viewPager.setAdapter(adapter);