Question

My result should be like showing images and text in each fragment. i am using fragments in the view pager

As images are high resolution, i am downloading it using async task and saving in sdcard .

If the images are downloaded , my code is working properly , If the images are not there , after downloading images , fragments are not getting refreshed and thus images are not getting displayed . How to refresh a fragment after completing image download.

My code in activity to set fragment is

   ExtendedViewPager pager = (ExtendedViewPager) findViewById(R.id.view_pager);

    /** Getting fragment manager */
    FragmentManager fm = getSupportFragmentManager();

    /** Instantiating FragmentPagerAdapter */
    pagerAdapter = new MyGalleryPagerAdapter(fm,eventArrayList);

    /** Setting the pagerAdapter to the pager object */
    pager.setAdapter(pagerAdapter);
    pager.setOnPageChangeListener(new MyPageChangeListener());
    pager.setCurrentItem(currentpage);

My adapter code is

public class MyGalleryPagerAdapter extends FragmentStatePagerAdapter implements{
 public static int mCurrentPage;
/** Constructor of the class 
 * @param eventArrayList */
ArrayList<EventsGalleryDetails> events;
public MyGalleryPagerAdapter(FragmentManager fm, ArrayList<EventsGalleryDetails>    eventArrayList) {
    super(fm);
    events=eventArrayList;
}

/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
    System.out.println("Get Item");
    MyGallery myFragment = new MyGallery();
    Bundle data = new Bundle();
    data.putInt("current_page", arg0);
    myFragment.setArguments(data);
    mCurrentPage = arg0;
    return myFragment;
}

public int currentItem(){
    return mCurrentPage;
}

public int getItemPosition(Object item) {
        return POSITION_NONE;

}
/** Returns the number of pages */
@Override
public int getCount() {     
    return events.size();
}

And my fragment code is

 public class MyGallery extends Fragment implements OnPageChangeListener{
ImageLoader imageLoader;
int mCurrentPage;
Bitmap bm ;
public static final String TAG = "MyGallery";
private LruCache<String, Bitmap> mMemoryCache;
private HashMap<Integer, TouchImageView>hmap = new HashMap<Integer, TouchImageView>();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    imageLoader=new ImageLoader(getActivity());
    Bundle data = getArguments();
    mCurrentPage = data.getInt("current_page", 0);
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // Use 1/8th of the available memory for this memory cache.
    final int cacheSize = maxMemory / 8;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Bundle data = getArguments();
    mCurrentPage = data.getInt("current_page", 0);
    View v = inflater.inflate(R.layout.gallerysecondscreenitem, container,false);               
    TouchImageView img = (TouchImageView)v.findViewById(R.id.galleryimage);
    TextView txt = (TextView ) v.findViewById(R.id.gallerytext);
    hmap.put(mCurrentPage, img);
    if(new File(Constants.Path+".jpg").exists()){
        loadBitmap(img);
        bm = null;
        img = null;
    }else{
        img.setImageResource(R.drawable.gallery_icon);
    }
    txt.setText(GallerySecondScreen.eventArrayList.get(mCurrentPage).getTitle());
    return v;       
}
public void loadBitmap(TouchImageView mImageView) {
     final String imageKey = String.valueOf(mCurrentPage);

        final Bitmap bitmap = mMemoryCache.get(imageKey);
        if (bitmap != null) {
            mImageView.setImageBitmap(bitmap);
        } else {
            mImageView.setImageResource(R.drawable.gallery_icon);
            BitmapWorkerTask task = new BitmapWorkerTask();
            task.execute(mImageView);
        }
}

My asyc task for downlaoding image is

         private class DownloadImageTask extends AsyncTask<Integer, Integer, Integer>{
          @Override
    protected Integer doInBackground(Integer... values) {
        try {
            for(int j=0;j<eventArrayList.size();j++){
                if(!new File(Constants.Path+".jpg").exists()){
                         downloadImageFromServer(eventArrayList.get(j).getImage(), ""+pos,""+j);
                    publishProgress(j);
                    //Log.d("Image will be downloaded from server now..", "the "+values[0]);
                }else{
                    publishProgress(j);
                }

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        Log.d("value of j:",""+j);
        return j;
    }}

Please suggest a way to refresh a view after completing my Image Download

Was it helpful?

Solution 3

i Resolved the issue by using the same pageradpter.notifydatasetchanged(). Instead of calling it every time , i called only when the image of current view got downloaded

Here is my modified OnprogressUpdate

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        System.out.println("Downloaded "+values[0]);
        if(values[0] == mPager.mCurrentPage){
            pagerAdapter.notifyDataSetChanged();
        }
    }

OTHER TIPS

try this.getView().draw() if they dont work for you try requestLayout() or forceLayout()

Here the issue is pager adapter is not getting refreshed. ImageView that was passed to BitmapWorkerTask will get updated after the image is downloaded but you have to find a way to update the pagerAdapter using notifyDataSetChanged in the PagerAdapter. First you need to create Callback from the BitmapWorkerTask to notify the fragment once the download is finished. And then you have to provide a listener back to your PagerAdapter which should call notifyDataSetChanged method once it gets triggered.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top