I split FeatrueListFragment in 2, creating BaseFragmentListFragment.
BaseFragmentListFragment contians all the list specific funtionaitly where FeatrueListFragment continans application specific funtions Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/fragment_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical" />
|
||||
|
||||
</ScrollView>
|
||||
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* BaseFragmentListFragment.java
|
||||
* @date May 24, 2012
|
||||
* @author ricky barrette
|
||||
* @author Twenty Codes, LLC
|
||||
*/
|
||||
package com.TwentyCodes.android.LocationRinger.ui.fragments;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.TwentyCodes.android.LocationRinger.debug.Debug;
|
||||
|
||||
/**
|
||||
* This fragment will be used to display a list of fragments
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public abstract class BaseFragmentListFragment extends Fragment {
|
||||
|
||||
private static final String TAG = "BaseFragmentListFragment";
|
||||
private final ArrayList<Fragment> mFragments;
|
||||
private final int mContainer;
|
||||
private final int mLayout;
|
||||
|
||||
/**
|
||||
* Creates a new BaseFragmentListFragment
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public BaseFragmentListFragment(ArrayList<Fragment> fragments, int layout, int container) {
|
||||
super();
|
||||
mFragments = fragments;
|
||||
mLayout = layout;
|
||||
mContainer = container;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(mContainer, 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(mContainer, 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) {
|
||||
return inflator.inflate(mLayout, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* (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() {
|
||||
if (Debug.DEBUG)
|
||||
Log.v(TAG, "onResume()");
|
||||
loadFragments();
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
@@ -7,17 +7,13 @@
|
||||
package com.TwentyCodes.android.LocationRinger.ui.fragments;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.ContentValues;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.media.AudioManager;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
@@ -27,19 +23,17 @@ import android.widget.ArrayAdapter;
|
||||
import com.TwentyCodes.android.LocationRinger.OnContentChangedListener;
|
||||
import com.TwentyCodes.android.LocationRinger.R;
|
||||
import com.TwentyCodes.android.LocationRinger.db.RingerDatabase;
|
||||
import com.TwentyCodes.android.LocationRinger.debug.Debug;
|
||||
|
||||
/**
|
||||
* This fragment will be used to display a list of fragments
|
||||
* This fragment will be used to display a list of features
|
||||
* TODO
|
||||
* + create button bar that had a plus button and a hint + add/remove features
|
||||
*
|
||||
* @author ricky
|
||||
*/
|
||||
public class FeatureListFragment extends Fragment implements OnClickListener, android.content.DialogInterface.OnClickListener {
|
||||
public class FeatureListFragment extends BaseFragmentListFragment implements OnClickListener, android.content.DialogInterface.OnClickListener {
|
||||
|
||||
private static final String TAG = "FeatureListFragment";
|
||||
private final ArrayList<Fragment> mFragments;
|
||||
private final ContentValues mInfo;
|
||||
private final OnContentChangedListener mListener;
|
||||
private final ArrayList<Integer> mAdded;
|
||||
@@ -52,47 +46,12 @@ public class FeatureListFragment extends Fragment implements OnClickListener, an
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public FeatureListFragment(ContentValues info, OnContentChangedListener listener, ArrayList<Fragment> fragments, ArrayList<Integer> added) {
|
||||
super();
|
||||
mFragments = fragments;
|
||||
super(fragments, R.layout.fragment_list_contianer, R.id.fragment_list_contianer);
|
||||
mInfo = info;
|
||||
mListener = listener;
|
||||
mAdded = added;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when an item is picked from the add featue list
|
||||
* (non-Javadoc)
|
||||
@@ -184,66 +143,12 @@ public class FeatureListFragment extends Fragment implements OnClickListener, an
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see android.support.v4.app.ListFragment#onCreateView(android.view.LayoutInflater,
|
||||
* android.view.ViewGroup, android.os.Bundle)
|
||||
* @see com.TwentyCodes.android.LocationRinger.ui.fragments.BaseFragmentListFragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
|
||||
*/
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle bundle) {
|
||||
final View v = inflator.inflate(R.layout.fragment_list_contianer, null);
|
||||
final View v = super.onCreateView(inflator, container, bundle);
|
||||
v.findViewById(R.id.add_feature_button).setOnClickListener(this);
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* (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() {
|
||||
if (Debug.DEBUG)
|
||||
Log.v(TAG, "onResume()");
|
||||
loadFragments();
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user