From 2fdbb2e5bb09cbbcef99a055188fa3239dbcb3bd Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Tue, 22 May 2012 21:21:56 -0400 Subject: [PATCH 1/6] Inital commit of the new dynamic Fragment list. This fragment no longer uses a listview or an adapter Signed-off-by: Ricky Barrette --- .../res/layout/fragment_list_contianer.xml | 20 +++--- .../ui/fragments/FeatureListFragment.java | 72 +++++++++---------- 2 files changed, 43 insertions(+), 49 deletions(-) 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..14a7381 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java @@ -11,19 +11,18 @@ import java.util.ArrayList; import android.content.ContentValues; import android.os.Bundle; 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.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; import com.TwentyCodes.android.LocationRinger.debug.Debug; +import com.TwentyCodes.android.LocationRinger.ui.fragments.FragmentListAdaptor.Holder; /** * This fragment will be used to display a list of fragments @@ -33,7 +32,16 @@ import com.TwentyCodes.android.LocationRinger.debug.Debug; * * @author ricky */ -public class FeatureListFragment extends ListFragment { +public class FeatureListFragment extends Fragment { + + + private static final String TAG = "FeatureListFragment"; + private final ArrayList mFragments; + + public FeatureListFragment(ContentValues info, OnContentChangedListener listener, ArrayList fragments) { + super(); + mFragments = fragments; + } /** * (non-Javadoc) @@ -42,51 +50,35 @@ public class FeatureListFragment extends ListFragment { * android.view.ViewGroup, android.os.Bundle) */ @Override - public View onCreateView(LayoutInflater inflator, ViewGroup container, - Bundle bundle) { + public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle bundle) { // TODO Auto-generated method stub - return super.onCreateView(inflator, container, bundle); - } - - 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; + + return inflator.inflate(R.layout.fragment_list_contianer, null); } @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); - } - - public boolean onContextItemSelected(MenuItem item) { - switch (item.getItemId()) { - case DELETE_ID: - Toast.makeText(this.getActivity(), "deleted! (note really)", - Toast.LENGTH_LONG).show(); - return true; + private void loadFragments() { + FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); + for(Fragment fragment : this.mFragments){ + transaction.add(R.id.fragment_list_contianer, fragment, fragment.getTag()); } - return super.onContextItemSelected(item); + transaction.commit(); + } + + /** + * Simple Holder class + * @author ricky barrette + */ + class Holder{ + public String tag; + public View view; } } \ No newline at end of file From d2c0e35a1bb45bfa1ccb13bcbaa4d66538052a99 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Tue, 22 May 2012 21:48:33 -0400 Subject: [PATCH 2/6] Added some simple add/remove methods, and I removed the FeatureListAdaptor Signed-off-by: Ricky Barrette --- .../ui/fragments/FeatureListFragment.java | 48 ++++-- .../ui/fragments/FragmentListAdaptor.java | 156 ------------------ 2 files changed, 33 insertions(+), 171 deletions(-) delete mode 100644 LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FragmentListAdaptor.java 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 14a7381..7722b73 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java @@ -11,9 +11,7 @@ import java.util.ArrayList; import android.content.ContentValues; import android.os.Bundle; 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; @@ -22,22 +20,26 @@ import android.view.ViewGroup; import com.TwentyCodes.android.LocationRinger.OnContentChangedListener; import com.TwentyCodes.android.LocationRinger.R; import com.TwentyCodes.android.LocationRinger.debug.Debug; -import com.TwentyCodes.android.LocationRinger.ui.fragments.FragmentListAdaptor.Holder; /** * 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 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; @@ -51,8 +53,6 @@ public class FeatureListFragment extends Fragment { */ @Override public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle bundle) { - // TODO Auto-generated method stub - return inflator.inflate(R.layout.fragment_list_contianer, null); } @@ -60,13 +60,16 @@ public class FeatureListFragment extends Fragment { public void onResume() { if (Debug.DEBUG) Log.v(TAG, "onResume()"); - loadFragments(); super.onResume(); } + /** + * Loads all the fragments + * @author ricky barrette + */ private void loadFragments() { - FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); + final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); for(Fragment fragment : this.mFragments){ transaction.add(R.id.fragment_list_contianer, fragment, fragment.getTag()); } @@ -74,11 +77,26 @@ public class FeatureListFragment extends Fragment { } /** - * Simple Holder class + * Adds the fragment to the list + * @param fragment * @author ricky barrette */ - class Holder{ - public String tag; - public View view; + 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(); + } + + /** + * 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(); } } \ 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 From c9f119c2e6ddba969bed776d6f0d352c11b623d3 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Wed, 23 May 2012 00:19:01 -0400 Subject: [PATCH 3/6] I overrided FeatureListFragment.onPause() to remove the fragments from the view. This allows them to be redrawn in onResume. The only issue that remains is that the fragments change order in which they are drawn in the UI. Signed-off-by: Ricky Barrette --- .../ui/fragments/FeatureListFragment.java | 74 +++++++++++++------ 1 file changed, 50 insertions(+), 24 deletions(-) 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 7722b73..0b59ee8 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java @@ -45,6 +45,33 @@ public class FeatureListFragment extends Fragment { 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){ + if(!fragment.isAdded()) + transaction.add(R.id.fragment_list_contianer, fragment, fragment.getTag()); + else + transaction.replace(R.id.fragment_list_contianer, fragment, fragment.getTag()); + } + transaction.commit(); + } + /** * (non-Javadoc) * @@ -56,37 +83,36 @@ public class FeatureListFragment extends Fragment { return inflator.inflate(R.layout.fragment_list_contianer, null); } + /** + * (non-Javadoc) + * @see android.support.v4.app.Fragment#onPause() + */ + @Override + public void onPause() { + final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); + for(Fragment fragment : this.mFragments){ + transaction.remove(fragment); + } + transaction.commit(); + super.onPause(); + } + @Override public void onResume() { if (Debug.DEBUG) Log.v(TAG, "onResume()"); loadFragments(); + + final View v = this.getView(); + if(v != null) + v.post(new Runnable(){ + @Override + public void run(){ + v.invalidate(); + } + }); super.onResume(); } - - /** - * 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(); - } - - /** - * 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(); - } /** * Removes a fragment from the list From 1ad1eb8b8c4e3e9885b140e5059f72c49da3a0ac Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Wed, 23 May 2012 00:31:18 -0400 Subject: [PATCH 4/6] I added a call to Collections.reverse() for mFragments in FeatureListFragment.onPause() This has fixed the issuse of the fragments reversing their order. The root cause is decribed here http://code.google.com/p/android/issues/detail?id=31116#makechanges Signed-off-by: Ricky Barrette --- .../LocationRinger/ui/fragments/FeatureListFragment.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 0b59ee8..a42cc7a 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java @@ -7,6 +7,7 @@ package com.TwentyCodes.android.LocationRinger.ui.fragments; import java.util.ArrayList; +import java.util.Collections; import android.content.ContentValues; import android.os.Bundle; @@ -74,7 +75,6 @@ public class FeatureListFragment extends Fragment { /** * (non-Javadoc) - * * @see android.support.v4.app.ListFragment#onCreateView(android.view.LayoutInflater, * android.view.ViewGroup, android.os.Bundle) */ @@ -94,9 +94,14 @@ public class FeatureListFragment extends Fragment { transaction.remove(fragment); } transaction.commit(); + Collections.reverse(this.mFragments); super.onPause(); } + /** + * (non-Javadoc) + * @see android.support.v4.app.Fragment#onResume() + */ @Override public void onResume() { if (Debug.DEBUG) From e552a77c71a52434999ea57dadc0bcda5a2fcaf6 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Wed, 23 May 2012 01:00:05 -0400 Subject: [PATCH 5/6] I have canged the call from transaction.commit for transaction/commitAllowingStateLoss() in FeatureListFragmetn.onPause() This has seemed to fix the issues with the fragments dissapearing after the ringtone activity returns. Signed-off-by: Ricky Barrette --- .../ui/fragments/FeatureListFragment.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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 a42cc7a..b9af79d 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java @@ -89,11 +89,17 @@ public class FeatureListFragment extends Fragment { */ @Override public void onPause() { - final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); - for(Fragment fragment : this.mFragments){ - transaction.remove(fragment); + try{ + final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); + for(Fragment fragment : this.mFragments){ + transaction.remove(fragment); + } + transaction.commitAllowingStateLoss(); + } catch(IllegalStateException e){ + e.printStackTrace(); + //do nothing } - transaction.commit(); + Collections.reverse(this.mFragments); super.onPause(); } From e2fd797162144493d6f10aaf8542d4594ff077ac Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Wed, 23 May 2012 12:10:29 -0400 Subject: [PATCH 6/6] Fixed fragments dissapearing on activity result I overrided FeatureListFragment.onActivtyResult() so I could remove and reload the fragments. Also in FeatureListFragment.loadFragments() I removed the if block that replaces fragments. Signed-off-by: Ricky Barrette --- .../ui/fragments/FeatureListFragment.java | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) 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 b9af79d..9d5ab43 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java @@ -10,6 +10,7 @@ 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.FragmentTransaction; @@ -64,15 +65,22 @@ public class FeatureListFragment extends Fragment { */ private void loadFragments() { final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); - for(Fragment fragment : this.mFragments){ - if(!fragment.isAdded()) - transaction.add(R.id.fragment_list_contianer, fragment, fragment.getTag()); - else - transaction.replace(R.id.fragment_list_contianer, fragment, fragment.getTag()); - } + 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, @@ -90,20 +98,15 @@ public class FeatureListFragment extends Fragment { @Override public void onPause() { try{ - final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); - for(Fragment fragment : this.mFragments){ - transaction.remove(fragment); - } - transaction.commitAllowingStateLoss(); + removeFragments(); } catch(IllegalStateException e){ e.printStackTrace(); //do nothing } - Collections.reverse(this.mFragments); super.onPause(); } - + /** * (non-Javadoc) * @see android.support.v4.app.Fragment#onResume() @@ -113,15 +116,6 @@ public class FeatureListFragment extends Fragment { if (Debug.DEBUG) Log.v(TAG, "onResume()"); loadFragments(); - - final View v = this.getView(); - if(v != null) - v.post(new Runnable(){ - @Override - public void run(){ - v.invalidate(); - } - }); super.onResume(); } @@ -136,4 +130,17 @@ public class FeatureListFragment extends Fragment { transaction.remove(fragment); transaction.commit(); } + + /** + * 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); + } + transaction.commit(); + } } \ No newline at end of file