Android Open Source - android Photo Gallery Fragment






From Project

Back to project page android.

License

The source code is released under:

MIT License

If you think the Android project android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.rdokollari.photogallery;
/*  w ww .  jav  a 2s  .  c  o  m*/
import java.io.IOException;
import java.util.ArrayList;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;

/**
 * 
 * @author Rizart Dokollari @ rdokollari.me
 * 
 */
public class PhotoGalleryFragment extends Fragment {
  private static final String TAG = "PhotoGalleryFragment";

  GridView mGridView;
  ArrayList<GalleryItem> mItems;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // retain the fragment
    setRetainInstance(true);
    // start AsyncTas, firing a background thread; call doInBackground
    new FetchItemsTask().execute();
  } // end method onCreate

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_photo_gallery, container,
        false);

    // get a reference to the GridView
    mGridView = (GridView) v.findViewById(R.id.gridView);

    return v;
  } // end method onCreateView

  /**
   * Because GridView has no handy GridFragment class, we need to build our
   * own adapter management code. setupAdapter() method looks at the current
   * model state and configures the adapter appropriately on the GridView. We
   * want to this to call in onCreateView(...), so that every time a new
   * GridView is created on rotation, it is reconfigures with an appropriate
   * adapter. We also want to call it every time our set of model objects
   * changes
   */
  private void setupAdapter() {
    if (getActivity() == null || mGridView == null)
      return;

    // make sure that the this fragment is still attached.
    if (mItems != null) {
      mGridView.setAdapter(new ArrayAdapter<GalleryItem>(getActivity(),
          android.R.layout.simple_gallery_item, mItems));
    } else {
      mGridView.setAdapter(null);
    }
  }

  /**
   * Uses background thread
   * 
   * @author Rizart Dokollari @ rdokollari.me
   * 
   */
  private class FetchItemsTask extends
      AsyncTask<Void, Void, ArrayList<GalleryItem>> {

    // get data form a web site and log it
    @Override
    protected ArrayList<GalleryItem> doInBackground(Void... params) {

      return new FlickrFetchr().fetchItems();
    } // end method doInBackground

    /**
     * Safe to update the UI form heres since onPostExecute(...) is is
     * called when doInBackground completes and runs on the main thread.
     */
    @Override
    protected void onPostExecute(ArrayList<GalleryItem> items) {
      mItems = items;
      setupAdapter();
    }
  } // end inner class FetchItemsTask

}




Java Source Code List

me.rdokollari.funququiz.QuoteActivity.java
me.rdokollari.funququiz.Quote.java
me.rdokollari.photogallery.FlickrFetchr.java
me.rdokollari.photogallery.GalleryItem.java
me.rdokollari.photogallery.PhotoGalleryActivity.java
me.rdokollari.photogallery.PhotoGalleryFragment.java
me.rdokollari.photogallery.SingleFragmentActivity.java