title: How to create Sliding images using view Pager in Fragment class?
body: <p>*I would like display full image from WebService and create sliding images at both forward and backward side. I have used ViewPager, PagerAdapter Class. 
  Fragment class is,</p>
  
  <pre><code>package com.Fragment.Classes;
  
  
  
  
    import java.util.ArrayList;
  
      import org.json.JSONArray;
      import org.json.JSONException;
      import org.json.JSONObject;
  
      import com.PersianBeauty.Adapter.ImagePagerAdapter;
      import com.persianbeauty.R;
      import com.utility.ConnectionDetector;
      import com.utility.JSONfunctions;
  
  
      import android.annotation.SuppressLint;
      import android.app.Fragment;
      import android.app.FragmentManager;
      import android.app.ProgressDialog;
      import android.os.AsyncTask;
      import android.os.Bundle;
      import android.support.v4.view.ViewPager;
      import android.util.Log;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.Toast;
  
      @SuppressLint("ValidFragment")
      public class ModelPortFoliolFragment extends Fragment
      {
          ImagePagerAdapter pagerAdapter;
  
          ViewPager pager;
          String[] imageUrls;
          View rootView;
          ArrayList&lt;String&gt; imageList;
          // Declare Variables
          JSONObject jsonobject;
          JSONArray jsonarray;
  
          // JSON Node names
          static  String TAG_MODEL = "model";
          static  String TAG_MODELIMAG1 = "ModelImage";
          static  String TAG_MODEL_IMAGE2 = "ModelImage_2";
          static  String TAG_MODEL_IMAGE3= "ModelImage_3";
  
          ProgressDialog mProgressDialog;
          String ID;
          public ModelPortFoliolFragment(String ID)
          {
              this.ID=ID;
          }
  
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
          { 
  
              rootView = inflater.inflate(R.layout.fragment_model_portfolio, container, false);
  
  
              if(!ConnectionDetector.networkStatus(getActivity())) 
              {
                  ConnectionDetector.displayNoNetworkDialog(getActivity());
              }
              else 
              {
                  new ParseModelPortFolioJSonData().execute();
              }
              pager = (ViewPager) rootView.findViewById(R.id.pager);
              //  pager.setAdapter(new ImagePagerAdapter(imageList,getActivity()));
              //pager.setCurrentItem(pagerPosition);
              pagerAdapter=new ImagePagerAdapter(imageList,getActivity());
              pager.setAdapter(pagerAdapter);
              //pager.setAdapter(new ImagePagerAdapter(imageList,getActivity()));
              //pager.setCurrentItem(pagerPosition);
  
  
              return rootView;
          }
      //  @Override
      //  public void onDestroyView()
      //  {
      //      super.onDestroyView();
      //
      //  }
          // DownloadJSON AsyncTask
          public class ParseModelPortFolioJSonData extends AsyncTask&lt;Void, Void, Void&gt;
          {   
  
  
              @Override
              protected void onPreExecute() 
              {
                  super.onPreExecute();
                  mProgressDialog = new ProgressDialog(getActivity());
                  mProgressDialog.setMessage("Please wait...");
                  //          mProgressDialog.setIndeterminate(false);
                  mProgressDialog.setCancelable(false);
                  mProgressDialog.show();
              }
  
  
              @Override
              protected Void doInBackground(Void... arg0)
  
              {   imageList=new ArrayList&lt;String&gt;();
              // Create an array
  
              try {
                  //http://www.persianbeautyawards.com/services/GetModelProfile.php?model_id=56
                  jsonobject = JSONfunctions.getJSONfromURL(getResources().getString(R.string.base_url).trim() +"GetModelPortfolio.php?model_id="+ID);
                  System.out.println(getResources().getString(R.string.base_url).trim() +"GetModelPortfolio.php?model_id="+ID+".....................");
                  // Locate the array name in JSON
                  System.out.println(jsonobject+".................");
                  if(jsonobject !=null)
                  {
                      jsonarray = jsonobject.getJSONArray(TAG_MODEL);
  
                      for (int i = 0; i &lt; jsonarray.length(); i++)
                      {
                          jsonobject = jsonarray.getJSONObject(i);
  
                          imageList.add(jsonobject.getString(TAG_MODELIMAG1));
                          System.out.println(jsonobject.getString(TAG_MODELIMAG1)+"...........");
  
                          imageList.add(jsonobject.getString(TAG_MODEL_IMAGE2));
                          System.out.println(jsonobject.getString(TAG_MODEL_IMAGE2)+"...........");
  
                          imageList.add(jsonobject.getString(TAG_MODEL_IMAGE3));
                          System.out.println(jsonobject.getString(TAG_MODEL_IMAGE3)+"...........");
  
                      }
  
                      // Set the JSON Objects into the array
                  }
  
  
              } catch (JSONException e) {
                  Log.e("Error", e.getMessage());
                  e.printStackTrace();
              }
              return null;
              }
  
              protected void onPostExecute(Void args) 
              {  
                  if(imageList !=null)
                  {  
                      pagerAdapter.notifyDataSetChanged();
                      if (rootView != null) {
                          ViewGroup parentViewGroup = (ViewGroup) rootView.getParent();
                          if (parentViewGroup != null) {
                              parentViewGroup.removeAllViews();
                          }
                      }
  
                  }
  
  
                  else
                  {
                      Toast.makeText(getActivity(),"Server Error try Again", Toast.LENGTH_LONG).show();
                  }
                  mProgressDialog.dismiss();
              }
          }
      }
  </code></pre>
  
  <p>I have used Adapter Class which extends from PageAdapter ,
   My PageAdapter class is,</p>
  
  <pre><code>PageAdapter:
  
   package com.PersianBeauty.Adapter;
  
  
  
  import java.util.ArrayList;
  
  import com.nostra13.universalimageloader.core.ImageLoader;
  import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
  import com.persianbeauty.R;
  
  import android.content.Context;
  import android.support.v4.view.PagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  
  
  
  public class ImagePagerAdapter extends PagerAdapter
  {
      LayoutInflater inflator;
      ImageView imgDisplay;
   Context context;
   ArrayList&lt;String&gt; slideShowImages;
   private ImageLoader imageLoader;
   public ImagePagerAdapter(ArrayList&lt;String&gt; slideShowImages,Context context)
   {
    this.context=context;
    this.slideShowImages =slideShowImages;
    imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(context));
   }
   @Override
   public int getCount() 
   {
    return slideShowImages.size();
   }
  
   @Override
   public boolean isViewFromObject(View view, Object object) {
    return view == ((ImageView) object);
   }
  
   @Override
   public Object instantiateItem(ViewGroup container, int position)
   {
       inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View viewLayout = inflator.inflate(R.layout.item_pager_view, container,
               false);
       imgDisplay = (ImageView) viewLayout.findViewById(R.id.image);
  //  BitmapFactory.Options options = new BitmapFactory.Options();
  //  options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  //  Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
  //  imgDisplay.setImageBitmap(bitmap);
  //   
  
    System.out.println("ImageUrl---------------------"+slideShowImages.get(position));
    imageLoader.displayImage(slideShowImages.get(position).trim(), imgDisplay);
    ((ViewPager) container).addView(imgDisplay, 0);
    return imgDisplay;
  
   }
  
   @Override
   public void destroyItem(ViewGroup container, int position, Object object)
   {
    ((ViewPager) container).removeView((ImageView) object);
   }
  }
  </code></pre>
  
  <p>Fragment Layout: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v4.view.ViewPager 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/pager"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" /&gt;
  </code></pre>
  
  <p>Image Layout which inflates imageLoader class,</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:padding="1dip" &gt;
  
      &lt;ImageView
          android:id="@+id/image"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="center"
          android:adjustViewBounds="true"
           /&gt;
  
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>when i run this code it gave following error  and not load any kind of image on view page,</p>
  
  <pre><code> 02-12 09:13:22.034: E/AndroidRuntime(3050): FATAL EXCEPTION: main
  02-12 09:13:22.034: E/AndroidRuntime(3050): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.view.ViewGroup.addView(ViewGroup.java:3210)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.support.v4.view.ViewPager.addView(ViewPager.java:1304)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.view.ViewGroup.addView(ViewGroup.java:3155)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at com.PersianBeauty.Adapter.ImagePagerAdapter.instantiateItem(ImagePagerAdapter.java:62)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:550)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:908)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2819)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at com.Fragment.Classes.ModelPortFoliolFragment$ParseModelPortFolioJSonData.onPostExecute(ModelPortFoliolFragment.java:151)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at com.Fragment.Classes.ModelPortFoliolFragment$ParseModelPortFolioJSonData.onPostExecute(ModelPortFoliolFragment.java:1)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.os.AsyncTask.finish(AsyncTask.java:631)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.os.Handler.dispatchMessage(Handler.java:99)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.os.Looper.loop(Looper.java:137)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at android.app.ActivityThread.main(ActivityThread.java:5041)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at java.lang.reflect.Method.invokeNative(Native Method)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at java.lang.reflect.Method.invoke(Method.java:511)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  02-12 09:13:22.034: E/AndroidRuntime(3050):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Please give me some suggestion.</p>