First Fragment Shown In ViewPager Is The Second One I Created
Solution 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());
Post a Comment for "First Fragment Shown In ViewPager Is The Second One I Created"