title: Android refresh a ViewPager inside a fragment
body: <p>I've tried to search inside SO before post but i'm not able to understand how to refresh the content of ViewPager hosted inside a fragment.
  Can you help me please?</p>
  
  <p>Thank you for your time</p>
  
  <p>Here my code.
  I've the fragment activity that host the ViewPager inside it. It show different widget and the ViewPager show different image, i call the returned list of image from the db "carosello" in the code. When i perform a search i call the refresh() method as a callback from the main activity that host this fragment and i setup again all views. Works fine for all the informations in the fragment, but do not works for the ViewPager: the oldest (first time created) image are always showed.</p>
  
  <pre><code>public class Page2FamigliaFragment extends Fragment {
  
  //... variable declaration
  
  @Override
  public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
  
      // fragment not when container null
      if (container == null) {
          return null;
      }
      // inflate view from layout
      View view = (LinearLayout)inflater.inflate(R.layout.famiglia_page2,container,false);
  
      //retrieve my object from the main view that host this fragment
      info = ((MainFamiglia)this.getActivity()).getMyObject();
  
     //run the setup of the view.    
     setUpMyPage2View(view);
  
     return view;
  }
  
  public void setUpMyPage2View(View view)
  {
  //... inizialization of some other objects and view - i do not show only because is out of topic for this matter
  //retrive the image used in the page viewer
      carosello = info.getCarosello();
      parts = carosello.split(",");
  
      //how many page ??
      int i = parts.length / 3;
      int resto = parts.length % 3;
      int pages = 0;
      if (resto!=0) {
          pages = i + 1;
      } else {
          pages = i;
      }
  
  List&lt;Fragment&gt; fragments = new ArrayList&lt;Fragment&gt;();
  
    String[] tempArray;
    int startIndex = 0;
    int endIndex = 3;
  
  
    if (parts.length &lt;= 3) {
  
      fragments.add(myCarosello.newInstance(parts));
  
    } else {
  
  
      tempArray = Arrays.copyOfRange(parts, startIndex, endIndex);
  
      for (int p=0; p &lt; pages; p++) {
  
          fragments.add(myCarosello.newInstance(tempArray));
          startIndex = endIndex;
          endIndex = ((endIndex+3) &gt; parts.length) ? parts.length : (endIndex+3);
          tempArray = Arrays.copyOfRange(parts, startIndex, endIndex);
  
      }
  
    }
  
    pageAdapter = new MyPageAdapter(getChildFragmentManager(), fragments);
  
    pager = (ViewPager) view.findViewById(R.id.viewpager);
    pager.setAdapter(pageAdapter);
    pager.setOnTouchListener(new OnTouchListener() {
  
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(
                    event.getAction() == MotionEvent.ACTION_DOWN &amp;&amp;
                    v instanceof ViewGroup
            ) {
                    ((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
            }
            return false;
        }
    });
  
  }
  
  //i call this method from a callback from the main view for refresh this fragment
  public void refresh(){
      //recupero oggetto dalla activity principale
      info = ((MainFamiglia)this.getActivity()).getMyObject();
      pager.getAdapter().notifyDataSetChanged();
      pager.removeAllViews();
      setUpMyPage2View(this.getView());
  }
  }
  </code></pre>
  
  <p>This is the class for the pageAdapter</p>
  
  <pre><code>public class MyPageAdapter extends FragmentPagerAdapter {
  
  // fragments to instantiate in the viewpager
     private List&lt;Fragment&gt; fragments;
  
     // constructor
     public MyPageAdapter(FragmentManager fm,List&lt;Fragment&gt; fragments) {
        super(fm);
        this.fragments = fragments;
     }
  
     // return access to fragment from position, required override
     @Override
     public Fragment getItem(int position) {
        return this.fragments.get(position);
     }
  
     // number of fragments in list, required override
     @Override
     public int getCount() {
        return this.fragments.size();
     }
  
     public int getItemPosition(Object object) {
          return POSITION_NONE;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and this is the class for the imageview inside the pageviewer</p>
  
  <pre><code>public class myCarosello extends Fragment {
  
  public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
  private int numeroImmagini = 0;
  
  public static final myCarosello newInstance(String[] pictures)
  {
      myCarosello f = new myCarosello();
      Bundle bdl = new Bundle(1);
      bdl.putStringArray(EXTRA_MESSAGE, pictures);
      f.setArguments(bdl);
  
      return f;
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
  
      String[] pictures = getArguments().getStringArray(EXTRA_MESSAGE);
      numeroImmagini = pictures.length;
  
      View view = (LinearLayout)inflater.inflate(R.layout.carosello,container,false);
  
      ImageView imageView1 = (ImageView) view.findViewById(R.id.carosello1);
      ImageView imageView2 = (ImageView) view.findViewById(R.id.carosello2);
      ImageView imageView3 = (ImageView) view.findViewById(R.id.carosello3);
  
      switch (numeroImmagini) {
      case 1:
          imageView1.setImageResource(returnImageResource(pictures[0]));
          imageView2.setVisibility(View.GONE);
          imageView3.setVisibility(View.GONE);
      break;
      case 2:
          imageView1.setImageResource(returnImageResource(pictures[0]));
          imageView2.setImageResource(returnImageResource(pictures[1]));
          imageView3.setVisibility(View.GONE);
      break;
      case 3:
          imageView1.setImageResource(returnImageResource(pictures[0]));
          imageView2.setImageResource(returnImageResource(pictures[1]));
          imageView3.setImageResource(returnImageResource(pictures[2]));
      break;
  
      }  
  
      return view;
  
  }
  
  //return the correct resource name
  private int returnImageResource(String image)
  {
      image = image.replace(" ", "_");
      String filenameArray[] = image.split("\\.");
      image = "img_" + filenameArray[0].toLowerCase();
      int drawableImageId = getActivity().getResources().getIdentifier(image, "drawable", getActivity().getPackageName());
      return drawableImageId;
  }
  }
  </code></pre>