我在 FragmentStatePagerAdapter 中遇到奇怪的问题。当我向前滑动时,它工作正常,当我向后滑动时它会跳过 2 个片段。如何解决这个问题?有什么方法可以获取当前项目编号吗?
导航页面适配器
public static class NavigationPagerAdapter extends FragmentStatePagerAdapter {
public NavigationPagerAdapter(android.support.v4.app.FragmentManager fm ) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new NaviagtionFragment();
Bundle args = new Bundle();
args.putInt("position", i); // Our object is just an integer :-P
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// For this contrived example, we have a 100-object collection.
return 100;
}
@Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position );
}
在片段中
public static class NaviagtionFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_pager, container, false);
Bundle args = getArguments();
int m= args.getInt("position");
Toast.makeText(c, ""+m, Toast.LENGTH_SHORT).show();//here i tracked the position of fragments when i swipe front ,its increasing 1,2,3,4,5,6,7 but when i swipe back it will go directly 7 -5 th position..
}}}