Android Open Source - PhotoGallery Photo Gallery Fragment






From Project

Back to project page PhotoGallery.

License

The source code is released under:

MIT License

If you think the Android project PhotoGallery 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 com.ambergleam.photogallery;
// w w  w . ja  v  a  2s.co  m
import java.util.ArrayList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SearchView;

public class PhotoGalleryFragment extends Fragment {

  private static final String TAG = "PhotoGalleryFragment";

  GridView mGridView;
  ArrayList<GalleryItem> mItems;
  ThumbnailDownloader<ImageView> mThumbnailThread;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "onCreate");

    setRetainInstance(true);
    setHasOptionsMenu(true);

    updateItems();

    Intent i = new Intent(getActivity(), PollService.class);
    getActivity().startService(i);

    mThumbnailThread = new ThumbnailDownloader<ImageView>(new Handler());
    mThumbnailThread.setListener(new ThumbnailDownloader.Listener<ImageView>() {
      public void onThumbnailDownloaded(ImageView imageView, Bitmap thumbnail) {
        if (isVisible()) {
          imageView.setImageBitmap(thumbnail);
        }
      }
    });
    mThumbnailThread.start();
    mThumbnailThread.getLooper();
    Log.i(TAG, "Background thread started");
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
    mGridView = (GridView) v.findViewById(R.id.gridView);
    setupAdapter();
    return v;
  }

  public void updateItems() {
    new FetchItemsTask().execute();
  }

  void setupAdapter() {
    if (getActivity() == null || mGridView == null) {
      return;
    }

    if (mItems != null) {
      mGridView.setAdapter(new GalleryItemAdapter(mItems));
    } else {
      mGridView.setAdapter(null);
    }
  }

  private class GalleryItemAdapter extends ArrayAdapter<GalleryItem> {
    public GalleryItemAdapter(ArrayList<GalleryItem> items) {
      super(getActivity(), 0, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
        convertView = getActivity().getLayoutInflater().inflate(R.layout.gallery_item, parent, false);
      }

      ImageView imageView = (ImageView) convertView.findViewById(R.id.gallery_item_imageView);
      imageView.setImageResource(R.drawable.brian_up_close);
      GalleryItem item = getItem(position);
      mThumbnailThread.queueThumbnail(imageView, item.getUrl());

      return convertView;
    }

  }

  private class FetchItemsTask extends AsyncTask<Void, Void, ArrayList<GalleryItem>> {
    @Override
    protected ArrayList<GalleryItem> doInBackground(Void... params) {
      Log.i(TAG, "doInBackground");

      Activity activity = getActivity();
      if (activity == null) {
        return new ArrayList<GalleryItem>();
      }

      String query = PreferenceManager.getDefaultSharedPreferences(activity).getString(FlickrFetchr.PREF_SEARCH_QUERY, null);

      if (query != null) {
        return new FlickrFetchr().search(query);
      } else {
        return new FlickrFetchr().fetchItems();
      }
    }

    @Override
    protected void onPostExecute(ArrayList<GalleryItem> items) {
      Log.i(TAG, "onPostExecute");
      mItems = items;
      setupAdapter();
    }
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    mThumbnailThread.quit();
    Log.i(TAG, "Background thread destroyed");
  }

  @Override
  public void onDestroyView() {
    super.onDestroyView();
    mThumbnailThread.clearQueue();
  }

  @Override
  @TargetApi(11)
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.fragment_photo_gallery, menu);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      // Pull out the SearchView
      MenuItem searchItem = menu.findItem(R.id.menu_item_search);
      SearchView searchView = (SearchView) searchItem.getActionView();

      // Get the data from our searchable.xml as a SearchableInfo
      SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
      ComponentName name = getActivity().getComponentName();
      SearchableInfo searchInfo = searchManager.getSearchableInfo(name);

      searchView.setSearchableInfo(searchInfo);
    }
  }

  @Override
  @TargetApi(11)
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_item_search:
      getActivity().onSearchRequested();
      return true;
    case R.id.menu_item_clear:
      PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString(FlickrFetchr.PREF_SEARCH_QUERY, null).commit();
      updateItems();
      return true;
    case R.id.menu_item_toggle_polling:
      boolean shouldStartAlarm = !PollService.isServiceAlarmOn(getActivity());
      PollService.setServiceAlarm(getActivity(), shouldStartAlarm);

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActivity().invalidateOptionsMenu();
      }

      return true;
    default:
      return super.onOptionsItemSelected(item);
    }
  }

  @Override
  public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    MenuItem toggleItem = menu.findItem(R.id.menu_item_toggle_polling);
    if (PollService.isServiceAlarmOn(getActivity())) {
      toggleItem.setTitle(R.string.stop_polling);
    } else {
      toggleItem.setTitle(R.string.start_polling);
    }
  }

}




Java Source Code List

com.ambergleam.photogallery.FlickrFetchr.java
com.ambergleam.photogallery.GalleryItem.java
com.ambergleam.photogallery.PhotoGalleryActivity.java
com.ambergleam.photogallery.PhotoGalleryFragment.java
com.ambergleam.photogallery.PollService.java
com.ambergleam.photogallery.SingleFragmentActivity.java
com.ambergleam.photogallery.ThumbnailDownloader.java