1

我在 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..


    }}}
4

1 回答 1

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);
            }
于 2013-11-10T15:38:09.490 回答