Added some simple add/remove methods, and I removed the

FeatureListAdaptor

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-05-22 21:48:33 -04:00
parent 2fdbb2e5bb
commit d2c0e35a1b
2 changed files with 33 additions and 171 deletions

View File

@@ -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<Fragment> mFragments;
/**
* Creates a new FeatureListFragment
* @param info
* @param listener
* @param fragments
* @author ricky barrette
*/
public FeatureListFragment(ContentValues info, OnContentChangedListener listener, ArrayList<Fragment> 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();
}
}

View File

@@ -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<Fragment> 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<Fragment> 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;
}
}