ActionBarSherlock + embedded fragments + ViewPager insider fragment

I have a tabbed application using ActionBarSherlock. Tabs in an ActionBar are every snippet. One of the fragments contains a viewpager that shows the number of fragments - each of which contains an image from the URL (using the ImageView option).

The whole thing works great the first time you click on a tab - it shows a pager that displays images. The second time you click a tab (after clicking on other tabs) nothing happens - the pager does not appear, and the screen remains blank (except for the actionBar). The code is attached below. It seems that the FragmentPagerAdapter is being created, but not paginated 2 times.

What am I doing wrong?

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.actionbarsherlock.app.SherlockFragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SurfaceChartFragment extends SherlockFragment {

private ViewPager mPager;
private SurfacePagerAdapter mAdapter;
private static String [] urls = { "gif url 1", "gif url 2 etc." };

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View V = inflater.inflate(R.layout.surface_pager, container, false);        
    mPager = (ViewPager) V.findViewById(R.id.surface_viewpager);
    mAdapter = new SurfacePagerAdapter(getActivity().getSupportFragmentManager());
    new setAdapterTask().execute();

    return V;
}

private class setAdapterTask extends AsyncTask<Void,Void,Void>{
    protected Void doInBackground(Void... params) {
          return null;
      }

      @Override
      protected void onPostExecute(Void result) {
          mPager.setAdapter(mAdapter);
      }
}


static final class SurfacePagerAdapter extends   FragmentPagerAdapter { // 
    public SurfacePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return urls.length;
    }

    @Override
    public Fragment getItem(int position) {
        SurfaceFragment f = new SurfaceFragment();

        f.url = urls[position];
        f.position = position;
        return f;
    }
}

public static class SurfaceFragment extends SherlockFragment {
    String url = "";
    Integer position = 0;

    public SurfaceFragment() {
       // setRetainInstance(true);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setHasOptionsMenu(true);
    }

     public static Bitmap getBitmapFromURL(String src) {

            try {

                URL url = new URL(src);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap myBitmap = BitmapFactory.decodeStream(input);

                return myBitmap;

            } catch (IOException e) {

                Log.e("Exception",e.getMessage());

                return null;
            }
        }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        ImageView image = new ImageView(getActivity());
        Bitmap bimage=  getBitmapFromURL(url);
        image.setImageBitmap(bimage);

        return image;

    }            

}

}
+5
1

, , (.. FragmentManager.add(), ViewPager PagerAdapter), getChildFragmentManager(), .

, :

new SurfacePagerAdapter(getActivity().getSupportFragmentManager());

:

new SurfacePagerAdapter(getChildFragmentManager());

, .

0

All Articles