0

In other topics as: How to implement a ViewPager with different Fragments / Layouts there are different fragment and different layout, each one selected in getItem method.

I've a lot of different layots (but all similar) and reused the same fragment. In my fragment I've this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{
    int pageLayout;
    switch (mPageNumber)
    {
    case 0:
        pageLayout = R.layout.fragment_screen_slide_page2;
        break;
    default:
        pageLayout = R.layout.fragment_screen_slide_page;
    }
    // Inflate the layout containing a title and body text.
    ViewGroup rootView = (ViewGroup) inflater
            .inflate(pageLayout, container, false);

    // Set the title view to show the page number.
    ((TextView) rootView.findViewById(android.R.id.text1)).setText(
            getString(R.string.title_template_step, mPageNumber + 1));

    return rootView;
}
/**
 * Factory method for this fragment class. Constructs a new fragment for the given page number.
 */
public static ScreenSlidePageFragment create(int pageNumber) {
    ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, pageNumber);
    fragment.setArguments(args);
    return fragment;
}

This works, is there something wrong with this approach instead of the classic approach that create a fragment for every layout?

Community
  • 1
  • 1
Accollativo
  • 1,537
  • 4
  • 32
  • 56

1 Answers1

1

I would recommend that instead of passing the page number to the Fragment and having the Fragment choose the layout based on the page number you use inversion of control and pass the layout resource to the fragment instead. The Fragment can then compare to see if the passed layout resource differs from the one it currently has inflated and change layouts.

This way you gain more control over the fragment in that you can pass any number of inter-convertible layouts instead of having to pass page numbers and having the fragment map the page number to a layout.

aashreys
  • 923
  • 1
  • 7
  • 11
  • Do you mean that in my create method I should add an int (to hold R.layout.fragment_screen_slide_page2 and other layouts), and I should move the switch in the adapter? – Accollativo Mar 14 '16 at 09:33
  • Essentially instead of 'create(int pageNumber)', use 'create(int layoutRes)' and then access the 'layoutRes' from your arguments and pass it to the layout inflater in the fragment. – aashreys Mar 14 '16 at 09:58
  • So I should leave all the code in this way except 'create(int layoutRes)'. Is it correct to instantiate every time a new fragment with a different id? Or could I implement a singleton and change the layout? (just for asking, this idea sound bad to me too). – Accollativo Mar 15 '16 at 08:09
  • Well I'd recommend against singletons since you'd have to manage fragment state manually for the most part. Fragments were designed to be modular lightweight ui components, so use them like that. Regarding the layout res you pass to your fragment, just use it in the layout inflater to inflate the UI you want and let the fragment handle it's lifecycle as usual. If you only change the layout and the layouts are absolutely intercompatible (similar structure, view ids, function etc.) then the fragment should be able to manage it's lifecycle without you to do anything. – aashreys Mar 16 '16 at 00:34