Java tutorial
/* * Copyright (C) 2012 Davy Leggieri * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package com.bydavy.card.receipts.fragments; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.bydavy.card.receipts.R; import com.bydavy.card.receipts.ReceiptPagerAdapter; import com.bydavy.card.receipts.providers.Receipts; import com.bydavy.card.receipts.utils.LogHelper; import com.bydavy.card.receipts.utils.LogHelper.ErrorID; public class ReceiptPagerFragment extends StateSherlockFragment { /** Change first statement of if condition to enable class debug **/ private static final boolean DEBUG = LogHelper.DEBUG ? false : LogHelper.DEFAULT_DEBUG; private static final String DEBUG_PREFIX = ReceiptPagerFragment.class.getName(); public interface OnReceiptPageChangeListener { void onReceiptPageSelected(int index); } private static final int CURSOR_LOADER_DEFAULT = 0; private static final String ARG_PAGE_INDEX = "page_index"; private static final String ARG_SELECTION = "selection"; private static final String ARG_SELECTION_ARGS = "selectionArgs"; private static final String ARG_SORTORDER = "sortOrder"; private static final String STATE_INDEX = "ReceiptPagerFragPage_index"; private static final String[] PROJECTION = new String[] { Receipts._ID, Receipts.COLUMN_NAME_SHOP }; private final MyPageLoader mLoaderCallBack = new MyPageLoader(); private final MyLegacyPageChangeListener mLegacyPageChangeListener = new MyLegacyPageChangeListener(); private OnReceiptPageChangeListener mOnPageChangeListener; private ViewPager mReceiptPager; private ReceiptPagerAdapter mAdapter; private int mIndex; private String mCursorSelection; private String[] mCursorSelectionArgs; private String mCursorSortOrder; /* * Browse all receipts */ public static ReceiptPagerFragment newInstance(int pageIndex) { return newInstance(pageIndex, null, null, null); } public static ReceiptPagerFragment newInstance(String selection, String[] selectionArgs, String sortOrder) { return newInstance(0, selection, selectionArgs, sortOrder); } /* * Browse a set of receipts */ public static ReceiptPagerFragment newInstance(int pageIndex, String selection, String[] selectionArgs, String sortOrder) { final ReceiptPagerFragment f = new ReceiptPagerFragment(); final Bundle args = new Bundle(); args.putInt(ARG_PAGE_INDEX, pageIndex); args.putString(ARG_SELECTION, selection); args.putStringArray(ARG_SELECTION_ARGS, selectionArgs); args.putString(ARG_SORTORDER, sortOrder); f.setArguments(args); return f; } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mOnPageChangeListener = (OnReceiptPageChangeListener) activity; } catch (final ClassCastException e) { LogHelper.e(ErrorID.IMPLEMENTATION, "Activity must implement " + OnReceiptPageChangeListener.class.getName(), e); throw e; } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Bundle args = getArguments(); if (args != null) { mCursorSelection = args.getString(ARG_SELECTION); mCursorSelectionArgs = args.getStringArray(ARG_SELECTION_ARGS); mCursorSortOrder = args.getString(ARG_SORTORDER); mIndex = args.getInt(ARG_PAGE_INDEX); } if (savedInstanceState != null) { mIndex = savedInstanceState.getInt(STATE_INDEX); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mReceiptPager = (ViewPager) inflater.inflate(R.layout.fragment_receipt_pager, null); mReceiptPager.setOnPageChangeListener(mLegacyPageChangeListener); return mReceiptPager; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mAdapter = new ReceiptPagerAdapter(getFragmentManager(), null); mReceiptPager.setAdapter(mAdapter); final Bundle bundle = mLoaderCallBack.createLoaderBundle(mCursorSelection, mCursorSelectionArgs, mCursorSortOrder); getLoaderManager().initLoader(CURSOR_LOADER_DEFAULT, bundle, mLoaderCallBack); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(STATE_INDEX, mIndex); } /* * Must be executed by the UI thread */ public void goToPage(int index) { if (DEBUG) { LogHelper.d(DEBUG_PREFIX + ": goToPage(" + index + ")"); } if (getLoaderManager().hasRunningLoaders()) { mIndex = index; } mReceiptPager.setCurrentItem(index, false); } /* * Must be executed by the UI thread */ public int getPageIndex() { return mIndex; } public Fragment getPrimaryFragment() { return mAdapter.getPrimaryItem(); } /* * Must be executed by the UI thread */ public long getPageReceiptId() { if (mAdapter != null) { return mAdapter.getItemId(getPageIndex()); } return 0; } public CharSequence getPageTitle(int position) { return mAdapter.getPageTitle(position); } private final class MyPageLoader implements LoaderCallbacks<Cursor> { private static final String BUNDLE_SELECTION = "selection"; private static final String BUNDLE_SELECTION_ARGS = "selectionArgs"; private static final String BUNDLE_SORT_ORDER = "sortOrder"; /* Not possible to create a static method =0/ */ public Bundle createLoaderBundle(String selection, String[] selectionArgs, String sortOrder) { final Bundle bundle = new Bundle(); bundle.putString(BUNDLE_SELECTION, selection); bundle.putStringArray(BUNDLE_SELECTION_ARGS, selectionArgs); bundle.putString(BUNDLE_SORT_ORDER, sortOrder); return bundle; } /* Dedicated thread */ @Override public Loader<Cursor> onCreateLoader(int id, Bundle bundle) { switch (id) { case CURSOR_LOADER_DEFAULT: if (bundle == null) { throw new IllegalArgumentException("Must set a bundle in the loader bundle"); } final String selection = bundle.getString(BUNDLE_SELECTION); final String[] selectionArgs = bundle.getStringArray(BUNDLE_SELECTION_ARGS); final String sortOrder = bundle.getString(BUNDLE_SORT_ORDER); return new CursorLoader(getActivity(), Receipts.CONTENT_URI, PROJECTION, selection, selectionArgs, sortOrder); default: throw new IllegalArgumentException("The cursor loader " + id + "doesn't exist"); } } /* UI Thread */ @Override public void onLoadFinished(Loader<Cursor> cl, Cursor cursor) { mAdapter.swapCursor(cursor); if (getState() != StateFragmentListener.STATE_COMPLETED) { if (mReceiptPager.getCurrentItem() != mIndex) { mReceiptPager.setCurrentItem(mIndex, false); } setState(StateFragmentListener.STATE_COMPLETED); } } /* UI Thread */ @Override public void onLoaderReset(Loader<Cursor> cl) { } } private class MyLegacyPageChangeListener implements android.support.v4.view.ViewPager.OnPageChangeListener { @Override public void onPageSelected(int index) { mIndex = index; if (mOnPageChangeListener != null) { mOnPageChangeListener.onReceiptPageSelected(index); } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } } }