1

I have list of objects Offer and with their data I try filling the fragments (one object per fragment). It works in the way that it shows them but the first one that is shown is really the second and after I swipe two times to the left and come back to the first then it is there (the first one)

I know that this question relates to this one

ViewPager first fragment shown is always wrong with FragmentStatePager

But the question is self answerd and not well understandable so I really need help

This is me code: Inside Fragment activity

    public class OffersDisplay extends FragmentActivity {
             /*some inicializations for layout and stuff*/

             listOfOffers = (ArrayList<Offer>) getIntent().getSerializableExtra("listOfOffers");
 ...
          public static class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);

        }

        @Override
        public int getCount() {
            return ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            return ImageFragment.init(listOfOffers.get(position));
        }

    }
}

And my ImageFragment class is seperate and looks like this

    public  class ImageFragment extends Fragment {
    //some inits again
        public static  ImageFragment init(Offer offer) {

            // Supply val input as an argument.
            name = offer.getName();
            description = offer.getDescription();
            moreInfoURL = offer.getMoreInfoURL();
     return new ImageFragment();
       }

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
         layoutView = inflater.inflate(R.layout.fragment_image, container,
                 false);
        tv = layoutView.findViewById(R.id.text);
        ((TextView) tv).setText(name + "\n"+ description);
        iview = layoutView.findViewById(R.id.image);

            //...some inits
        ((ImageView) iview).setImageBitmap(pic);

        return layoutView ;
    }

}

Community
  • 1
  • 1
user1480742
  • 145
  • 12
  • Any one has any insights? I too am facing the same issue with FragmentStatePagerAdapter and Viewpager. Pls help. – lini sax Jun 21 '15 at 18:26
  • The linked question's suggestion solutions don't work. I've tried with a simple String array a,b,c, d and I get pages b,b,c,d and swiping backwards from c or d, -> c,b,a or d,c,b,a. FIRST element's oncreateView/ onCreate() is never called – lini sax Jun 21 '15 at 19:08

1 Answers1

1

I have fixed this issue. The reason why it was breaking is that for some reason (not clear to me) you cannot initialize values required by the Fragment (ImageFRagment in your eg.) , in it's init or factory method and expect to use it later while creating views to keep the order in place. You MUST supply the values as arguments to the fragment via arguments(bundle). Hence, if adapter has 4 pages with content A, B, C, D when you start the pager, the onCreateView and onCreate will be called for element[0] ie. A. In my case i'd always get B, B, C, D and swiping back D, C, B , A.

so to fix your code `public static ImageFragment init(Offer offer) {

    ImageFragment fragment = new ImageFragment();
    Bundle args = new Bundle();


    String name = offer.getName();
    String description = offer.getDescription();

    args.putString(ARG_NAME, name);
    args.putString(ARG_CONTENT, description);


    fragment.setArguments(args);

    return fragment; }

then on your onCreate() you must initialize the values that you need for your views using the arguments:

 mName = getArguments().getInt(ARG_NAME);
 content = getArguments().getString(ARG_CONTENT);

In your onCreateView(), you can access these values using helper functions to retrieve them.

((TextView) rootView.findViewById(android.R.id.text1)).setText(getContent()); ((TextView) rootView.findViewById(R.id.article_content)).setText(getPageNumber());

lini sax
  • 911
  • 1
  • 11
  • 15