在选项卡式活动中添加额外片段

时间:2015-04-23 12:10:44

标签: android android-fragments

我创建了一个带有选项卡式活动的新项目,然后我创建了2个额外的片段,我想将它们添加到我的项目中,这段代码如果在创建活动后从MainActivity中添加,我该如何将我的2个片段添加到此代码中?

var alamofireManager : Alamofire.Manager?

func getCallToServer(){
  let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
  configuration.timeoutIntervalForResource = 10 // seconds

  self.alamofireManager = Alamofire.Manager(configuration: configuration)
  self.alamofireManager!.request(.GET, "http://example.com/")
    .response { (request, response, data, error) in

    }
}

2 个答案:

答案 0 :(得分:0)

 public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class
            // below).
            return PlaceholderFragment.newInstance(position + 1);

            // Check position and create instance of newly created fragments here

        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }

答案 1 :(得分:0)

您需要做的就是更改SectionsPagerAdapter类,如下所示:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class
        // below).

        if (position == 3)
           return FirstFragment.newInstance();    // Instantiate the first fragment here
        if (position == 4)
           return SecondFragment.newInstance();    // Instantiate the second fragment here
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages. (+2 more)
        return 5;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        case 3:
            return "First fragment title"; // TODO change here
        case 4:
            return "Second fragment title"; // TODO change here
        }
        return null;
    }
}