Android Open Source - Recipe-Puppy-Android Recipe View Pager Fragment






From Project

Back to project page Recipe-Puppy-Android.

License

The source code is released under:

Apache License

If you think the Android project Recipe-Puppy-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 com.jerin.magicrecipe.fragments;
//from  w  w w . java 2  s.c  o m
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jerin.magicrecipe.MainActivity;
import com.jerin.magicrecipe.R;
import com.jerin.magicrecipe.adapters.RecipePagerAdapter;
import com.jerin.magicrecipe.data.MagicRecipeConstants;
import com.jerin.magicrecipe.data.RecipeItem;
import com.jerin.utilities.RequestTask;
import com.jerin.utilities.RequestTask.IRequestResponseListener;
import com.jerin.utilities.Utilities;

/**
 * Fragment to show recipes in a viewpager.
 * 
 * @author jerin
 * 
 */
public class RecipeViewPagerFragment extends Fragment implements
    IRequestResponseListener {

  private final String _TAG = RecipeViewPagerFragment.class.getSimpleName();

  private RequestTask mRequestTask;

  /**
   * The pager widget, which handles animation and allows swiping horizontally
   * to access previous and next wizard steps.
   */
  private ViewPager mPager;

  /**
   * The pager adapter, which provides the pages to the view pager widget.
   */
  private RecipePagerAdapter mPagerAdapter;

  public RecipeViewPagerFragment() {
  }

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

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) fragmentView.findViewById(R.id.pager);
    mPagerAdapter = new RecipePagerAdapter(getFragmentManager());
    mPager.setAdapter(mPagerAdapter);

    sendRequest();
    return fragmentView;
  }

  /**
   * Execute HTTP request to fetch recipes.
   */
  private void sendRequest() {
    Bundle arguments = getArguments();
    Uri query = Utilities.buildUri(
        MagicRecipeConstants.MAGICRECIPE_BASE_URL,
        MagicRecipeConstants.MAGICRECIPE_URL_PATH, arguments);
    mRequestTask = new RequestTask();
    mRequestTask.setListener(this);
    mRequestTask.execute(query.toString());
  }

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((MainActivity) activity).onSectionAttached(getArguments().getInt(
        MagicRecipeConstants.MAGICRECIPE_TAB_KEY));
  }

  @Override
  public void onRequestComplete(String response) {
    // Response received. Parse and update ViewPagerAdapter.
    Log.d(_TAG, response);

    ArrayList<RecipeItem> recipeItems = parseDataFrom(response);
    mPagerAdapter.setData(recipeItems);
  }

  /**
   * Method to parse Recipe List from JSON response.
   * 
   * @param response
   * @return
   */
  private ArrayList<RecipeItem> parseDataFrom(String response) {
    ArrayList<RecipeItem> recipeList = new ArrayList<RecipeItem>();

    JSONObject jsonObject;
    if (null != response && (response.trim().length() > 0)) {
      try {
        jsonObject = new JSONObject(response);

        JSONArray jsonArray = jsonObject
            .getJSONArray(MagicRecipeConstants.MAGICRECIPE_JSON_KEY_RESULTS);

        if (null != jsonArray) {
          int size = jsonArray.length();

          for (int index = 0; index < size; index++) {
            JSONObject jRecipeItem = jsonArray.getJSONObject(index);

            String title = jRecipeItem
                .getString(MagicRecipeConstants.MAGICRECIPE_JSON_KEY_TITLE);
            String url = jRecipeItem
                .getString(MagicRecipeConstants.MAGICRECIPE_JSON_KEY_IMAGE_URL);
            String ingredients = jRecipeItem
                .getString(MagicRecipeConstants.MAGICRECIPE_JSON_KEY_INGREDIENTS);

            RecipeItem recipeItem = new RecipeItem();
            recipeItem.setTitle(title);
            recipeItem.setImageUrl(url);
            recipeItem.setIngredients(ingredients);
            recipeList.add(recipeItem);
          }
        }
      } catch (JSONException e) {
        Log.e(_TAG, e.getMessage());
        e.printStackTrace();
      }
    }

    return recipeList;
  }

  @Override
  public void onDestroyView() {
    if (null != mRequestTask) {
      mRequestTask.setListener(null);
      mRequestTask.cancel(true);
    }
    super.onDestroyView();
  }
}




Java Source Code List

com.jerin.magicrecipe.MainActivity.java
com.jerin.magicrecipe.NavigationDrawerFragment.java
com.jerin.magicrecipe.adapters.RecipePagerAdapter.java
com.jerin.magicrecipe.data.MagicRecipeConstants.java
com.jerin.magicrecipe.data.RecipeItem.java
com.jerin.magicrecipe.fragments.RecipePageFragment.java
com.jerin.magicrecipe.fragments.RecipeSearchFragment.java
com.jerin.magicrecipe.fragments.RecipeViewPagerFragment.java
com.jerin.utilities.RequestTask.java
com.jerin.utilities.Utilities.java