我在FragmentStatePagerAdapter中有一个奇怪的问题。当我向前滑动时,它会工作gud,当我向后滑动它跳过2个碎片时如何解决这个问题? 有没有办法让当前项目没有?
NavigationPagerAdapter
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..
}}}
答案 0 :(得分:1)
Toast非常慢并且在您浏览视图时不是很好的解决方案,因为它在屏幕上停留的时间更长并且无法显示所有消息,因此最好检查LogCat中的位置。尝试使用此项目位置并使用日志查看位置:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("position") : 1;
Log.i("tag", "Item clicked: " + mNum);
}