3

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

Daniel
  • 305
  • 4
  • 14

1 Answers1

5

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.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • Yes! that's it! It worked for me. The only thing I changed in your code is that I need to work with the position of the page and not the title, because the application is multi language. In the class that extends the Fragment Adapter, I changed to: public Fragment getItem(int position) { return baseFragment.newInstance(String.valueOf(position));} – Daniel May 22 '12 at 13:32
  • yeah. That's rite. I understood that it is not a good practice, but since the question was asked a several hours ago, I had to go for a fast solution. I am happy it works for you. – Andro Selva May 22 '12 at 13:34
  • Seems that when I'm on page 4 for example, the contents of page 1 and 2 is reloaded and I lose some information. Do you know any way to avoid this? – Daniel May 22 '12 at 13:54
  • What kind of information are you losing? I just did a sample based on it. I am not aware of it actually. – Andro Selva May 22 '12 at 14:01
  • I did a test, I put a button on page 2, when clicked it changes the self text to another text... So I scroll to page 4, and when I back to page 2, the button text is the original :( ... It would be better if all pages are loaded at once. – Daniel May 22 '12 at 14:15
  • well actually the code works like this, each time you make a scroll the view gets null and draws a new view. So What you have to do is, declare the number of views required(say 5 views if you have 5 fragments). And now in onCreateView, check for null, and if null populate your view else just display that view which you have declared globally. should solve the problem,. – Andro Selva May 22 '12 at 14:18
  • I understand, but looking at the documentation [http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)] ... I found a simpler solution to do what I need. mPager.setOffscreenPageLimit(5); The default is 1 (adjacents - I think). Thank you again! – Daniel May 22 '12 at 14:32
  • The real problem with this solution is the reloading when you move between pages. – Idan Gozlan Jan 29 '13 at 13:38
  • your code workds nice. @AndroSelva can you please answer http://stackoverflow.com/questions/22961427/touch-or-click-event-viewpagerindicator-in-android. i stuck here since days – Manwal Apr 09 '14 at 12:03