我如何使用活动的数组列表而不是使用多个片段

时间:2013-07-24 09:15:16

标签: android android-fragments android-viewpager fragmentpageradapter android-pageradapter

在我的项目中关于使用swipe查看多个活动我已经使用了片段的想法和我的代码工作得非常好,但是今天早上我的经理让我将片段的数组列表转换为数组活动列表 但我真的不知道我怎么能做到这一点请任何帮助这是我的代码

public class MainActivity extends FragmentActivity {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    List < Fragment > fragments;

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

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        fragments = new Vector < Fragment > ();

        fragments.add(Fragment.instantiate(this, FragmentOne.class.getName()));
        fragments.add(Fragment.instantiate(this, FragmentTwo.class.getName()));
        fragments.add(Fragment.instantiate(this, FragmentThree.class.getName()));
        fragments.add(Fragment.instantiate(this, FragmentFour.class.getName()));
        fragments.add(Fragment.instantiate(this, FragmentFive.class.getName()));
        fragments.add(Fragment.instantiate(this, FragmentSix.class.getName()));

        mSectionsPagerAdapter = new SectionsPagerAdapter(super.getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            int _pos = position % 6;
            return fragments.get(_pos);
        }

        @
        Override
        public int getCount() {
            // Show 3 total pages.
            return 6;
        }

        @
        Override
        public CharSequence getPageTitle(int position) {
            final String title_section4 = "Section4";
            final String title_section5 = "Section5";
            final String title_section6 = "Section6";
            final String title_section1 = "Section1";
            final String title_section2 = "Section2";
            final String title_section3 = "Section3";

            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return title_section1.toUpperCase(l);
                case 1:
                    return title_section2.toUpperCase(l);
                case 2:
                    return title_section3.toUpperCase(l);
                case 3:
                    return title_section4.toUpperCase(l);
                case 4:
                    return title_section5.toUpperCase(l);
                case 5:
                    return title_section6.toUpperCase(l);
            }

            return null;
        }
    }

    public static class FragmentOne extends Fragment {

        public static final String ARG_SECTION_NUMBER = "section_number";

        public FragmentOne() {}

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.one, container, false);

            return rootView;
        }
    }

    public static class FragmentTwo extends Fragment {

        public static final String ARG_SECTION_NUMBER = "section_number";

        public FragmentTwo() {}

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.two, container, false);

            return rootView;
        }
    }

    public static class FragmentThree extends Fragment {
        public static final String ARG_SECTION_NUMBER = "section_number";

        public FragmentThree() {}

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.three, container, false);

            return rootView;
        }
    }

    public static class FragmentFour extends Fragment {

        public static final String ARG_SECTION_NUMBER = "section_number";

        public FragmentFour() {}

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

    public static class FragmentFive extends Fragment {

        public static final String ARG_SECTION_NUMBER = "section_number";

        public FragmentFive() {}

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

    public static class FragmentSix extends Fragment {

        public static final String ARG_SECTION_NUMBER = "section_number";

        public FragmentSix() {}

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

1 个答案:

答案 0 :(得分:0)

您进入MainActivity的项目使用Fragments no Activities

我认为如果你想使用Swipe视图,你不能使用Array Activities,因为Swipe View它基于ViewPager并且需要片段。

所以我觉得用“活动”“滑动视图”是不可能的。

抱歉我的英文。

相关问题