FragmentPagerAdapter Exists Only In Android.Support.V4.App (and not Android.App)

后端 未结 5 870
灰色年华
灰色年华 2020-12-12 14:54

I cannot find find FragmentPagerAdapter within Android.App.

I do not want to use the Fragment\'s from Android.Support.V4.App, as my target API is 14 and higher (Andr

5条回答
  •  既然无缘
    2020-12-12 15:21

    Add this dependecy to the gradle dependencies:

    compile 'com.android.support:support-v13:+'
    

    And use android.support.v13.app.FragmentPagerAdapter like this (I simply modified the official demo project in android studio: file → new → new project → next → next → tabbed activity → next → finish):

    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.support.v13.app.FragmentPagerAdapter;
    import com.google.android.gms.maps.MapFragment;
    
    /** A simple FragmentPagerAdapter that returns a MapFragment and a PreferenceFragment. */
    public class MainActivityAdapter extends FragmentPagerAdapter {
    
        private MapFragment mapFragment;
        private PreferencesFragment preferencesFragment;
    
        public MainActivityAdapter(FragmentManager fm) {
            super(fm);
            mapFragment = MapFragment.newInstance();
            preferencesFragment = new PreferencesFragment();
        }
    
        @Override
        public int getCount() {
            return 2;
        }
    
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return mapFragment;
                case 1:
                    return preferencesFragment;
                default:
                    return null;
            }
        }
    }
    

提交回复
热议问题