Android: ViewPagerIndicator - Creating different layouts for different pages

android, android-viewpager, viewpagerindicator

Solution

I found a simple solution which works for me. Here is what I did.

TestFragment.class

      public static TestFragment newInstance(String content) {
        TestFragment fragment = new TestFragment();
//
//        StringBuilder builder = new StringBuilder();
//        for (int i = 0; i < 20; i++) {
//            builder.append(content).append(" ");
//        }
//        builder.deleteCharAt(builder.length() - 1);
        fragment.mContent = content;

        return fragment;
    }

Comment out the for loop here. Just get the mContent which we are going to use as a Flag.

Now in your `onCreateView()` change it as follows,

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        TextView text = new TextView(getActivity());
//        text.setGravity(Gravity.CENTER);
//        text.setText(mContent);
//        text.setTextSize(20 * getResources().getDisplayMetrics().density);
//        text.setPadding(20, 20, 20, 20);
//
//        LinearLayout layout = new LinearLayout(getActivity());
//        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//        layout.setGravity(Gravity.CENTER);
//        layout.addView(text);
        Log.i("mContent",mContent);
        View view=null;
        if(mContent.equalsIgnoreCase("title1"))
        {
            view = inflater.inflate(R.layout.one, container, false);

        }
        else if(mContent.equalsIgnoreCase("title2"))
        {
            view = inflater.inflate(R.layout.two, container, false);
        }
        else if(mContent.equalsIgnoreCase("title3"))
        {
            view = inflater.inflate(R.layout.three, container, false);
        }
        return view;

    }

That is all it takes. Now you will be able to inflate your views based on the Title name which we have used as the flag.

Problem

I need to create four different layouts, each one to a page of ViewPagerIndicator. How can I do this? The ViewPagerIndicator is already working, but I'm using the sample from http://viewpagerindicator.com and there is created a simple TextView for all pages. See the sample (TestFragment.java): ``` @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView text = new TextView(getActivity()); text.setGravity(Gravity.CENTER); text.setText(mContent); text.setTextSize(20 * getResources().getDisplayMetrics().density); text.setPadding(20, 20, 20, 20); LinearLayout layout = new LinearLayout(getActivity()); layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); layout.setGravity(Gravity.CENTER); layout.addView(text); return layout; } ``` I need to identify the current page (position) and refer to a related resource layout (XML). Is it possible? I also need to ensure that all Views of all pages to be loaded at once only one time when I create the activity, allowing values ​​to be updated later. I appreciate any help!! Thanks

Original source