kandroidj
kandroidj

Reputation: 13932

How would i implement a Generic ArrayList of Fragments in Android?

So I am trying to implement a method that uses all of my Fragment classes to create an Array List<? extends Fragment> in my MainActivity.java class which is an ArrayList with different objects that extend the Fragment class.

The primary reason why I am doing this is to load fragments using the android.support.v4.jar support library ViewPager view.

Here is what i Have so far, but I cannot add the different Objects that extend Fragment into my ArrayList

public List<? extends Fragment> getFragments(){
    List<? extends Fragment> ipsumFragments = new ArrayList<Fragment>();
    ipsumFragments.add(BaconIpsumFragment.newInstance());
    ipsumFragments.add(BuseyIpsumFragment.newInstance());
    ipsumFragments.add(LoremIpsumFragment.newInstance());

    return ipsumFragments;
}

I am just trying to get my head around using a ViewPager and multiple fragments.If anyone could help me I would greatly appreciate it.

And here is my class for The first Fragment, the others are exactly the same but named different and use different XML layouts

public class BaconIpsumFragment extends Fragment{

public static final BaconIpsumFragment newInstance(){

    BaconIpsumFragment baconFragment = new BaconIpsumFragment();
    Bundle args = new Bundle();
    baconFragment.setArguments(args);
    return baconFragment;   
}

@Override
public void onAttach(Activity activity){
    // Attempt to attach to the parent activity
    super.onAttach(activity);
}

// Called when the parent activity creates the fragment
@Override
public void onCreate(Bundle savedInstanceState){
    // Perform the default behavior
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    super.onCreateView(inflater, container, savedInstanceState);
    return inflater.inflate(R.layout.bacon_fragment, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    // Last attempt to change any variables
    super.onActivityCreated(savedInstanceState);
}

}

And the full implementation of my activity. My error is occurring in the getFragments() method when trying to add an instance of a fragment.

public class MainActivity extends FragmentActivity {

private ActionBar tabbedActionBar = null;   
private String [] ipsumsArray = null;
private Resources resources = null;
private FragmentManager manager = null;
private FragmentTransaction transaction = null;

private List<Fragment> mIpsumFragments = null;
private LoremIpsumViewPager pagerAdapter = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get the resources object
    resources = getResources();
    // get the String array from resources
    ipsumsArray = resources.getStringArray(R.array.ipsums_array);

    // Get all the fragments that we will attempt to display
    mIpsumFragments = getFragments();

    // get a reference to the action bar
    tabbedActionBar = getActionBar();
    // set the navigation mode for the action bar
    tabbedActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for (int i = 0; i < ipsumsArray.length; i++){
        Tab tab = tabbedActionBar.newTab();
        tab.setText(ipsumsArray[i]);
        tab.setTabListener(tabListener);
        tabbedActionBar.addTab(tab);
    }

//      setDefaultTab();


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private TabListener tabListener = new TabListener(){

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction arg1) {

        // Get the fragment manager
        manager = getFragmentManager();
        transaction = manager.beginTransaction();


        int tabSelected = tab.getPosition();


    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

};



private List<Fragment> getFragments(){
    List<Fragment> ipsumFragments = new ArrayList<Fragment>();

    ipsumFragments.add(BaconIpsumFragment.newInstance());
    ipsumFragments.add(BuseyIpsumFragment.newInstance());
    ipsumFragments.add(LoremIpsumFragment.newInstance());

    return ipsumFragments;
}

}

Upvotes: 0

Views: 6430

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213311

There is no need to use wildcard at all. Just declare your list as:

List<Fragment> ipsumFragments = new ArrayList<Fragment>();

It doesn't restrict you to add just the instances of Fragment in the list. You will be able to add instances of subclasses of Fragment in it too. And then change the return type of your method to List<Fragment>.

The use of declaring a list as List<? extends Fragment> is that you can assign that reference, list of any subclass of Fragment. So, you can assign List<BaconIpsumFragment>, List<LoremIpsumFragment>, that's it. And that is the reason why you can't add instances of any subclass in such lists, because you don't know what list do you have actually underneath. So, you might be trying to add a BaconIpsumFragment into a List<LoremIpsumFragment>, which is certainly not correct.

Upvotes: 4

Related Questions