title: ViewPager with Fragments holding GridView
body: <p>I have a ViewPager with two Fragments , each Fragment has a GridView</p>
  
  <p>Frag1.java</p>
  
  <pre><code>public class Frag1 extends Fragment{
  GridView grid;
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      return inflater.inflate(R.layout.frag1, container, false);
  }
  
  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      grid = (GridView) getActivity().findViewById(R.id.gridView1);
      grid.setAdapter(new MyAdapter());
  
  }
  
  private class MyAdapter extends BaseAdapter {
      private int ar []={R.drawable.ch1,R.drawable.ch2,R.drawable.ch3,R.drawable.ch4};
      private LayoutInflater inflater;
  
  
      @Override
      public int getCount() {
          return ar.length;
      }
  
      @Override
      public Object getItem(int i) {
          return ar[i];
      }
  
      @Override
      public long getItemId(int i) {
          return i;
      }
  
      @Override
      public View getView(int i, View view, ViewGroup viewGroup) {
          View v = view;
          ViewHolder holder = null;
  
          if(v == null) {
              inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              v = inflater.inflate(R.layout.grid_layout, viewGroup, false);
              holder = new ViewHolder();
              v.setTag(holder);
  
          }else{
              holder = (ViewHolder) v.getTag();
          }
  
          holder.img = (ImageView)v.findViewById(R.id.imageView1);
          holder.img.setBackgroundResource(ar[i]);
          return v;
      }
  
      private class ViewHolder{
          ImageView img;
      }
  }
  </code></pre>
  
  <p>Frag2 is the same as Frag1 but with two different things</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      return inflater.inflate(R.layout.frag, container, false);
  }
  private class MyAdapter extends BaseAdapter {
      private int ar []={R.drawable.gmc1,R.drawable.gmc2,R.drawable.gmc3,R.drawable.gmc4};
       ......
     }
  </code></pre>
  
  <p>This is SecondActivity which contain the ViewPager</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_second);
      pager = (ViewPager) findViewById(R.id.pager);
      FragmentManager fm = getSupportFragmentManager();
      adapter = new MyPagerAdapter(fm);
      pager.setAdapter(adapter);
  
  }
  
   class MyPagerAdapter extends FragmentStatePagerAdapter {
  
      public MyPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
  
          Fragment frag;
          if (position == 0) {
              frag = new Frag1();
          } else {
              frag = new Frag2();
          }
          return frag;
  
      }
  
      @Override
      public int getCount() {
          // TODO Auto-generated method stub
          return 2;
      }
  }
  </code></pre>
  
  <p>The problem is ViewPage view only one page (Frag2) and when scroll the page it get a blank page
  <img src="https://i.stack.imgur.com/rc5ko.jpg" alt="enter image description here">    <img src="https://i.stack.imgur.com/sKDK0.jpg" alt="enter image description here"></p>