diff --git a/LocationRinger/res/layout/fragment_list_contianer.xml b/LocationRinger/res/layout/fragment_list_contianer.xml index 4786db8..5d0a8ef 100644 --- a/LocationRinger/res/layout/fragment_list_contianer.xml +++ b/LocationRinger/res/layout/fragment_list_contianer.xml @@ -1,10 +1,12 @@ - - - + + + + + \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java index ae0c638..9d5ab43 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java @@ -7,19 +7,17 @@ package com.TwentyCodes.android.LocationRinger.ui.fragments; import java.util.ArrayList; +import java.util.Collections; import android.content.ContentValues; +import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; -import android.support.v4.app.ListFragment; +import android.support.v4.app.FragmentTransaction; import android.util.Log; -import android.view.ContextMenu; -import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; -import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.Toast; import com.TwentyCodes.android.LocationRinger.OnContentChangedListener; import com.TwentyCodes.android.LocationRinger.R; @@ -27,66 +25,122 @@ import com.TwentyCodes.android.LocationRinger.debug.Debug; /** * This fragment will be used to display a list of fragments - * - * TODO + create button bar that had a plus button and a hint + add/remove - * features + * TODO + * + create button bar that had a plus button and a hint + add/remove features * * @author ricky */ -public class FeatureListFragment extends ListFragment { +public class FeatureListFragment extends Fragment { + + private static final String TAG = "FeatureListFragment"; + private final ArrayList mFragments; + + /** + * Creates a new FeatureListFragment + * @param info + * @param listener + * @param fragments + * @author ricky barrette + */ + public FeatureListFragment(ContentValues info, OnContentChangedListener listener, ArrayList fragments) { + super(); + mFragments = fragments; + } + + /** + * Adds the fragment to the list + * @param fragment + * @author ricky barrette + */ + public void add(final Fragment fragment){ + this.mFragments.add(fragment); + final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); + transaction.add(R.id.fragment_list_contianer, fragment, fragment.getTag()); + transaction.commit(); + } + + /** + * Loads all the fragments + * @author ricky barrette + */ + private void loadFragments() { + final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); + for(Fragment fragment : this.mFragments) + transaction.add(R.id.fragment_list_contianer, fragment, fragment.getTag()); + transaction.commit(); + } + + /** + * (non-Javadoc) + * @see android.support.v4.app.Fragment#onActivityResult(int, int, android.content.Intent) + */ + @Override + public void onActivityResult(int arg0, int arg1, Intent arg2) { + removeFragments(); + loadFragments(); + super.onActivityResult(arg0, arg1, arg2); + } /** * (non-Javadoc) - * * @see android.support.v4.app.ListFragment#onCreateView(android.view.LayoutInflater, * android.view.ViewGroup, android.os.Bundle) */ @Override - public View onCreateView(LayoutInflater inflator, ViewGroup container, - Bundle bundle) { - // TODO Auto-generated method stub - return super.onCreateView(inflator, container, bundle); + public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle bundle) { + return inflator.inflate(R.layout.fragment_list_contianer, null); } - private static final String TAG = "FeatureListFragment"; - private static final int DELETE_ID = 0; - private ArrayList mFeatures; - - // private OnContentChangedListener mListener; - // private ContentValues mInfo; - // private int mIndex; - - public FeatureListFragment(ContentValues info, - OnContentChangedListener listener, ArrayList fragments) { - super(); - this.mFeatures = fragments; - // this.mInfo = info; - // this.mListener = listener; + /** + * (non-Javadoc) + * @see android.support.v4.app.Fragment#onPause() + */ + @Override + public void onPause() { + try{ + removeFragments(); + } catch(IllegalStateException e){ + e.printStackTrace(); + //do nothing + } + Collections.reverse(this.mFragments); + super.onPause(); } + /** + * (non-Javadoc) + * @see android.support.v4.app.Fragment#onResume() + */ @Override public void onResume() { - this.setListAdapter(new FragmentListAdaptor(this, mFeatures)); - this.getListView().setOnCreateContextMenuListener(this); if (Debug.DEBUG) Log.v(TAG, "onResume()"); + loadFragments(); super.onResume(); } - - public void onCreateContextMenu(ContextMenu menu, View v, - ContextMenuInfo menuInfo) { - super.onCreateContextMenu(menu, v, menuInfo); - menu.add(0, DELETE_ID, 0, R.string.delete).setIcon( - android.R.drawable.ic_menu_delete); + + /** + * Removes a fragment from the list + * @param fragment + * @author ricky barrette + */ + public void remove(final Fragment fragment){ + this.mFragments.remove(fragment); + final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); + transaction.remove(fragment); + transaction.commit(); } - - public boolean onContextItemSelected(MenuItem item) { - switch (item.getItemId()) { - case DELETE_ID: - Toast.makeText(this.getActivity(), "deleted! (note really)", - Toast.LENGTH_LONG).show(); - return true; + + /** + * Removes all fragments from the the view + * @throws IllegalStateException + * @author ricky barrette + */ + private void removeFragments() throws IllegalStateException { + final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); + for(Fragment fragment : this.mFragments){ + transaction.remove(fragment); } - return super.onContextItemSelected(item); + transaction.commit(); } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FragmentListAdaptor.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FragmentListAdaptor.java deleted file mode 100644 index b237cf7..0000000 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FragmentListAdaptor.java +++ /dev/null @@ -1,156 +0,0 @@ -/** - * FragmentListAdaptor.java - * @date Dec 24, 2011 - * @author ricky barrette - * @author Twenty Codes, LLC - */ -package com.TwentyCodes.android.LocationRinger.ui.fragments; - -import java.util.ArrayList; - -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentTransaction; -import android.support.v4.app.ListFragment; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; - -import com.TwentyCodes.android.LocationRinger.R; -import com.TwentyCodes.android.LocationRinger.debug.Debug; - -/** - * This Adaptor Class will be used to display fragments in a ListFragment. - * TODO - * + Get this code working - * + Add/Remove fragments on the fly - * @author ricky barrette - */ -public class FragmentListAdaptor extends BaseAdapter { - - private LayoutInflater mInflater; - private ArrayList mFragments; - private FragmentManager mFragmentManager; - private final String TAG = "FragmentListAdaptor"; - - /** - * Creates a new FragmentListAdaptor - * @param listFragment - * @param fragments - * @author ricky barrette - */ - public FragmentListAdaptor(ListFragment listFragment, ArrayList fragments) { - if(Debug.DEBUG) - Log.v(TAG, "FragmentListAdaptor()"); - mInflater = LayoutInflater.from(listFragment.getActivity()); - mFragments = fragments; - mFragmentManager = listFragment.getFragmentManager(); - } - - /** - * Returns the number of Fragments to display - * (non-Javadoc) - * @see android.widget.Adapter#getCount() - */ - @Override - public int getCount() { - if(Debug.DEBUG) - Log.v(TAG, "getCount() :"+ mFragments.size()); - return mFragments.size(); - } - - /** - * Returns the fragment to display - * (non-Javadoc) - * @see android.widget.Adapter#getItem(int) - */ - @Override - public Fragment getItem(int position) { - if(Debug.DEBUG) - Log.v(TAG, "getItem("+position+")"); - return mFragments.get(position); - } - - /** - * Returns the id of the fragment being displayed - * (non-Javadoc) - * @see android.widget.Adapter#getItemId(int) - */ - @Override - public long getItemId(int position) { - return position; - } - - /** - * (non-Javadoc) - * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) - */ - @Override - public View getView(int position, View convertView, ViewGroup parent) { - if(Debug.DEBUG) - Log.v(TAG, "getView("+position+")"); - FragmentTransaction transaction = mFragmentManager.beginTransaction(); - - // A Holder keeps references to children views to avoid unnecessary calls to findViewById() on each row. - Holder holder; - - /* - * When convertView is not null, we can reuse it directly, there is no need - * to reinflate it. We only inflate a new View when the convertView supplied - * by ListView is null. - */ - if (convertView == null) { - convertView = mInflater.inflate(R.layout.fragment_container, null); - - /* - * Creates a ViewHolder and store references - * that we want to bind data to. - */ - holder = new Holder(); - holder.view = (View) convertView.findViewById(R.id.fragment_container); - holder.view.setId(position+1); - holder.tag = createTag(position); - convertView.setTag(holder); - - //add the fragment to the new view -// transaction.add(holder.view.getId(), getItem(position), holder.tag); - transaction.replace(holder.view.getId(), getItem(position),holder.tag); - } else { - // Get the ViewHolder back to get fast access to the Old Views - holder = (Holder) convertView.getTag(); - Fragment shown = mFragmentManager.findFragmentByTag(holder.tag); - - //replace the old fragment with a new one - transaction.addToBackStack(holder.tag); - holder.tag = createTag(position); - if(shown != null) - transaction.remove(shown); -// transaction.add(holder.view.getId(), getItem(position), holder.tag); - transaction.replace(holder.view.getId(), getItem(position),holder.tag); - } - transaction.commit(); - - return convertView; - } - - /** - * @param position - * @return a unique tag to be used for identifying fragments - * @author ricky barrette - */ - private String createTag(int position){ - return "andorid:FragmentList:tag:"+position; - } - - /** - * Simple Holder class - * @author ricky barrette - */ - class Holder{ - public String tag; - public View view; - } - -} \ No newline at end of file