Android Open Source - Race2GED2 Class View Pager Fragment






From Project

Back to project page Race2GED2.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Def...

If you think the Android project Race2GED2 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 2014 Regional Adult Education Program of Lee, Scott, Wise, and Norton Public Schools
 */*from   ww w .j a va 2 s .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 edu.mecc.race2ged.fragments;

import android.app.Activity;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.viewpagerindicator.TitlePageIndicator;
import java.util.ArrayList;
import edu.mecc.race2ged.JSON.County;
import edu.mecc.race2ged.JSON.Region;
import edu.mecc.race2ged.R;
import edu.mecc.race2ged.activities.HomeActivity;
import edu.mecc.race2ged.adapters.ClassViewPagerAdapter;

/**
 * <code>ClassesFragment</code> shows all counties via a ViewPager. Each page has that counties
 * class information.
 *
 * @author Bryan Smith
 * @date 5/26/2014.
 */
public class ClassViewPagerFragment extends Fragment {

    private Region mRegion = null;
    private ArrayList<String> titles = new ArrayList<String>();
    private ArrayList<Fragment> frags = new ArrayList<Fragment>();
    private static final String ARG_REGION = "regionParam";
    private ClassViewPagerAdapter classViewPagerAdapter;
    private TitlePageIndicator titleIndicator;
    private ViewPager pager;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param reg The region object that stores the class data used.
     *
     * @return A new instance of fragment ClassesFragment.
     */
    public static ClassViewPagerFragment newInstance(Region reg) {
        ClassViewPagerFragment fragment = new ClassViewPagerFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_REGION, reg);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * Construct a <code>ClassesFragment</code>
     */
    public ClassViewPagerFragment() {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null){
            mRegion = (Region)getArguments().getSerializable(ARG_REGION);
        }
        if (mRegion != null) populate(mRegion);
        classViewPagerAdapter = new ClassViewPagerAdapter(getFragmentManager(),titles,frags);
    }

    /**
     * Called to ask the fragment to save its current dynamic state, so it
     * can later be reconstructed in a new instance of its process is
     * restarted.  If a new instance of the fragment later needs to be
     * created, the data you place in the Bundle here will be available
     * in the Bundle given to {@link #onCreate(android.os.Bundle)},
     * {@link #onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)}, and
     * {@link #onActivityCreated(android.os.Bundle)}.
     * <p/>
     * <p>This corresponds to {@link android.app.Activity#onSaveInstanceState(android.os.Bundle)
     * Activity.onSaveInstanceState(Bundle)} and most of the discussion there
     * applies here as well.  Note however: <em>this method may be called
     * at any time before {@link #onDestroy()}</em>.  There are many situations
     * where a fragment may be mostly torn down (such as when placed on the
     * back stack with no UI showing), but its state will not be saved until
     * its owning activity actually needs to save its state.
     *
     * @param outState Bundle in which to place your saved state.
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(ARG_REGION,mRegion);
    }

    @Override
    public void onResume() {
        super.onResume();
            ((HomeActivity)getActivity()).getSupportActionBar().setTitle("Classes");
    }

    /**
     * Get the data ready for each county from the region.
     * @param reg The region data to use.
     */
    private void populate(Region reg){
        if ( reg != null){
            final int size = reg.getCounties().size();
            County c;
            for (int i = 0; i < size; i++)
            {
                c = reg.getCounties().get(i);
                getTitles().add(c.getName());
                getFrags().add(ClassCardListFragment.newInstance(c));
            }
        }
    }

    /**
     * {@inheritDoc}
     * <p>
     *     Inflates and populates all of the counties fragments
     * </p>
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_classes, container, false);

        //Set the pager with an adapter
        pager = (ViewPager)view.findViewById(R.id.pager);
        pager.setAdapter(classViewPagerAdapter);

        //Bind the title indicator to the adapter
        titleIndicator = (TitlePageIndicator)view.findViewById(R.id.titles);
        titleIndicator.setSelectedColor(Color.BLACK);
        titleIndicator.setFooterColor(getResources().getColor(R.color.content_color));
        titleIndicator.setBackgroundColor(Color.WHITE);
        titleIndicator.setSelectedBold(true);
        titleIndicator.setTextColor(Color.GRAY);
        titleIndicator.setViewPager(pager);
        return view;
    }


    /**
     * Get the currently stored region data for the fragment.
     * @return The region data object currently stored.
     */
    public Region getRegion() {
        return mRegion;
    }

    /**
     * Sets the current region data object
     * @param reg The region object that stores the class data used.
     */
    public void setRegion(Region reg) {
        this.mRegion = reg;
    }

    /**
     * Get the list of titles currently stored.
     * @return List of titles currently stored.
     */
    public ArrayList<String> getTitles() {
        return titles;
    }

    /**
     * Sets the lists of titles to use.
     * @param titles Lists of titles to use.
     */
    public void setTitles(ArrayList<String> titles) {
        this.titles = titles;
    }

    /**
     * Get the list of fragments currently stored.
     * @return List of fragments currently stored.
     */
    public ArrayList<Fragment> getFrags() {
        return frags;
    }

    /**
     * Sets the lists of fragments to use.
     * @param frags Lists of fragments to use.
     */
    public void setFrags(ArrayList<Fragment> frags) {
        this.frags = frags;
    }
}




Java Source Code List

edu.mecc.race2ged.GEDApplication.java
edu.mecc.race2ged.JSON.Class.java
edu.mecc.race2ged.JSON.County.java
edu.mecc.race2ged.JSON.Instructor.java
edu.mecc.race2ged.JSON.Region.java
edu.mecc.race2ged.JSON.State.java
edu.mecc.race2ged.activities.HomeActivity.java
edu.mecc.race2ged.activities.SettingsActivity.java
edu.mecc.race2ged.activities.SplashScreen.java
edu.mecc.race2ged.adapters.CardAdapter.java
edu.mecc.race2ged.adapters.ClassViewPagerAdapter.java
edu.mecc.race2ged.cards.ClassCard.java
edu.mecc.race2ged.cards.ExpandableCard.java
edu.mecc.race2ged.cards.StubCard.java
edu.mecc.race2ged.fragments.CardListFragment.java
edu.mecc.race2ged.fragments.ClassCardListFragment.java
edu.mecc.race2ged.fragments.ClassViewPagerFragment.java
edu.mecc.race2ged.helpers.ClassDataReader.java
edu.mecc.race2ged.helpers.ClassDataUpdater.java
edu.mecc.race2ged.helpers.SettingsHelper.java
edu.mecc.race2ged.helpers.Utils.java
edu.mecc.race2ged.navigation.ActionBarDrawerToggleHoneycomb.java
edu.mecc.race2ged.navigation.ActionBarDrawerToggle.java
edu.mecc.race2ged.navigation.DrawerLayout.java
edu.mecc.race2ged.navigation.NavDrawerListAdapter.java
edu.mecc.race2ged.navigation.NavDrawerListItemNoIcon.java
edu.mecc.race2ged.navigation.NavDrawerListItemWithIcon.java
edu.mecc.race2ged.navigation.NavDrawerListItem.java
edu.mecc.race2ged.navigation.NavigationDrawerFragment.java
edu.mecc.race2ged.widgets.Card.java
edu.mecc.race2ged.widgets.Header.java