Search code examples
androidandroid-fragmentsandroid-viewpagerfragmentstatepageradapter

The FragmentPagerAdapter update the pages wrongly?


I am having strange issue in FragmentStatePagerAdapter.when i swipe front ,it works gud,when i swipe back it skip 2 fragments.how to resolve this? is there any way to get current item no?

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

in fragment

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


    }}}

Solution

  • Toast is very slow and it is not good solution when you are swiping views, because it remains on the screen longer and can not reach to show all messages, so its better to check position in LogCat. Try this for items position and see position with Log:

        @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    mNum = getArguments() != null ? getArguments().getInt("position") : 1;
    Log.i("tag", "Item clicked: " + mNum);
                }