Android Open Source - ChicagoWaterWalk-Android Routes Grid Fragment






From Project

Back to project page ChicagoWaterWalk-Android.

License

The source code is released under:

Apache License

If you think the Android project ChicagoWaterWalk-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

/*
 * Copyright (C) 2014 The Illinois-Indiana Sea Grant
 */*from  www .j  a  va  2s . c o m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.iisgcp.waterwalk.fragment;

import java.util.ArrayList;
import java.util.List;

import org.iisgcp.waterwalk.R;
import org.iisgcp.waterwalk.activity.AboutActivity;
import org.iisgcp.waterwalk.activity.PointOfInterestActivity;
import org.iisgcp.waterwalk.adapter.RouteGridAdapter;
import org.iisgcp.waterwalk.adapter.RowItem;
import org.iisgcp.waterwalk.utils.Constants;
import org.iisgcp.waterwalk.utils.ImageFetcher;
import org.iisgcp.waterwalk.utils.Utils;
import org.iisgcp.waterwalk.utils.ImageCache.ImageCacheParams;

import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class RoutesGridFragment extends Fragment {
  
    private static final String IMAGE_CACHE_DIR = "route_thumbs";
  
    private RouteGridAdapter mAdapter;
    private ImageFetcher mImageFetcher;
    
  private GridView mList;
  
    /**
     * Fragment initialization.  We way we want to be retained and
     * start our thread.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Tell the framework to try to keep this fragment around
        // during a configuration change.
        setRetainInstance(true);
        setHasOptionsMenu(true);
        
        ImageCacheParams cacheParams = new ImageCacheParams(getActivity(), IMAGE_CACHE_DIR);
        cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory

        // The ImageFetcher takes care of loading images into our ImageView children asynchronously
        mImageFetcher = new ImageFetcher(getActivity(), Utils.getLargestGridDimension(getActivity()));
        mImageFetcher.setLoadingImage(R.drawable.empty_photo);
        mImageFetcher.addImageCache(getActivity().getSupportFragmentManager(), cacheParams);
    }
  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
      super.onCreateView(inflater, container, savedInstanceState);
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_routes_grid, container, false);
        mList = (GridView) view.findViewById(R.id.gridview);
        return view;
    }
    
    public void onActivityCreated (Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
        
        List<RowItem> rowItems = null;
        
        TypedArray routes = getResources().obtainTypedArray(R.array.routes);
        
        List<Integer> imageIds = new ArrayList<Integer>();
      
        rowItems = new ArrayList<RowItem>();
        
        for (int i = 0; i < routes.length(); i++) {
          TypedArray route = getResources().obtainTypedArray(routes.getResourceId(i, -1));
          
          String title = getString(route.getResourceId(0, -1));
          int imageId = route.getResourceId(1, -1);

          imageIds.add(imageId);
          
          rowItems.add(buildRow(imageId, title, routes.getResourceId(i, -1)));
          route.recycle();
        }
        
        routes.recycle();
        
        mAdapter = new RouteGridAdapter(getActivity(), mImageFetcher, rowItems);
        
        mList.setAdapter(mAdapter);
        mList.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position,
          long id) {
        RowItem rowItem = (RowItem) parent.getAdapter().getItem(position);
        
        Intent intent = new Intent(RoutesGridFragment.this.getActivity(), PointOfInterestActivity.class);
            intent.putExtra(Constants.INTENT_TITLE, rowItem.getTitle());
            intent.putExtra(Constants.INTENT_RES_ID, rowItem.getResId());
          intent.putExtra(Constants.INTENT_TAB, 0);
          intent.putExtra(Constants.INTENT_POSITION, position);
          intent.putExtra(Constants.INTENT_DRAWER_POSITION, 0);
        RoutesGridFragment.this.startActivity(intent);
      }
          
        });
    }
    
    @Override
    public void onResume() {
        super.onResume();
        mImageFetcher.setExitTasksEarly(false);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPause() {
        super.onPause();
        mImageFetcher.setPauseWork(false);
        mImageFetcher.setExitTasksEarly(true);
        mImageFetcher.flushCache();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        mImageFetcher.closeCache();
    }
    
  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.routes_grid_fragment_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
          case R.id.menu_about:
            getActivity().startActivity(new Intent(getActivity(), AboutActivity.class));
            return true;
          default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    private RowItem buildRow(int imageId, String title, int resId) {
      RowItem rowItem = new RowItem();
      rowItem.setImageId(imageId);
      rowItem.setTitle(title);
      rowItem.setResId(resId);
      
      return rowItem;
    }
}




Java Source Code List

org.iisgcp.waterwalk.activity.AboutActivity.java
org.iisgcp.waterwalk.activity.FactActivity.java
org.iisgcp.waterwalk.activity.MainActivity.java
org.iisgcp.waterwalk.activity.MapActivity.java
org.iisgcp.waterwalk.activity.NavigationDrawerActivity.java
org.iisgcp.waterwalk.activity.PictureActivity.java
org.iisgcp.waterwalk.activity.PointOfInterestActivity.java
org.iisgcp.waterwalk.activity.PointOfInterestDetailActivity.java
org.iisgcp.waterwalk.adapter.AllRoutesAdapter.java
org.iisgcp.waterwalk.adapter.DetailListAdapter.java
org.iisgcp.waterwalk.adapter.RecyclingImageView.java
org.iisgcp.waterwalk.adapter.RouteGridAdapter.java
org.iisgcp.waterwalk.adapter.RowItem.java
org.iisgcp.waterwalk.fragment.AllRoutesFragment.java
org.iisgcp.waterwalk.fragment.FactFragment.java
org.iisgcp.waterwalk.fragment.FactViewPagerFragment.java
org.iisgcp.waterwalk.fragment.MapFragment.java
org.iisgcp.waterwalk.fragment.PointOfInterestGridFragment.java
org.iisgcp.waterwalk.fragment.PointOfInterestGridViewPagerFragment.java
org.iisgcp.waterwalk.fragment.PointOfInterestMapFragment.java
org.iisgcp.waterwalk.fragment.RoutesGridFragment.java
org.iisgcp.waterwalk.fragment.RoutesMapFragment.java
org.iisgcp.waterwalk.utils.AsyncTask.java
org.iisgcp.waterwalk.utils.Constants.java
org.iisgcp.waterwalk.utils.CustomImageView.java
org.iisgcp.waterwalk.utils.DiskLruCache.java
org.iisgcp.waterwalk.utils.GenericDialog.java
org.iisgcp.waterwalk.utils.HelpDialog.java
org.iisgcp.waterwalk.utils.ImageCache.java
org.iisgcp.waterwalk.utils.ImageFetcher.java
org.iisgcp.waterwalk.utils.ImageResizer.java
org.iisgcp.waterwalk.utils.ImageWorker.java
org.iisgcp.waterwalk.utils.InfoDialog.java
org.iisgcp.waterwalk.utils.LicenseDialog.java
org.iisgcp.waterwalk.utils.OnItemClickedListener.java
org.iisgcp.waterwalk.utils.OnPageSelectedListener.java
org.iisgcp.waterwalk.utils.PictureInfoDialog.java
org.iisgcp.waterwalk.utils.RecyclingBitmapDrawable.java
org.iisgcp.waterwalk.utils.Utils.java