简体   繁体   中英

How can I extend Fragment and have a subclass extend my base class along with ListFragment?

To clarify my question, I would like to have a base class that extends Fragment, such as:

public class MyFragment extends Fragment
{
    @Override
    public void onAttach(Activity activity)
    {
        //common work
    }
}

I have sub classes extending MyFragment but I would also like to have sub classes gaining the functionality of ListFragment, which already extends Fragment.

How can I accomplish this without having to create 2 base classes, one for simple Fragment and another for ListFragment (and possibly a third for DialogFragment), or having to look into the source of ListFragment (and DialogFragment) in order to create my own subclasses that extend MyFragment?

You can't. A class can have only one base class.

Your best course of aciton is to forget about ListFragment . It doesn't do very much anyway.

Their is one way to do this, to create a ListFragment ( public class SecondFragment extends ListFragment ) then you call it as child fragment from your base fragment MyFragment . This is the way to create fragment inside of frgment(nesting of framents). Please refer these links.. http://developer.android.com/reference/android/app/Fragment.html and http://developer.android.com/about/versions/android-4.2.html#NestedFragments

ListFragment just provides some boilerplate code, if you really want to have its functions you can check out its source code for reference and implement something similar into your MyFragment. It's one of the perks of developing on Android to be able to check platform sourcecode to see how it does things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM