-1

I used the "fixed tabs + swipe" navigation and I want to set Activities content and logic to each tab. I've been searching a lot, and didn't find any solution.

I have the following:

public class ManejadorTabs extends FragmentActivity implements
    ActionBar.TabListener {


SectionsPagerAdapter mSectionsPagerAdapter;

ViewPager mViewPager;

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

    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);


    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }

}

@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;
}

@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int i) {
        Fragment fragment; 
        switch(i){
        case 0:
            fragment = new Fragment1();
            break;
        case 1:
             fragment = new Fragment1();
             break;
        case 2:
             fragment = new Fragment2();
             break;
        default:
             throw new IllegalArgumentException("Invalid section number");
        }

        //set args if necessary
      //  Bundle args = new Bundle();
        //args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
        //fragment.setArguments(args);

        return fragment;
    }

    @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 "Tab1";
        case 1:
            return "T2";
        case 2:
            return "T3";
        }
        return null;
    }
}

How do I "convert" my activities into fragments, in order to display them on each tab? Please help!

4

1 回答 1

1

没有通用的方法可以将活动转换为片段。一般来说,您的onCreate()逻辑将倾向于转换为一种onCreateView()方法,您可以在其中返回 UI(而不是 call setContentView())。其他生命周期方法(例如,onPause())应该按原样工作。您的其余活动有多少变化,从“不多”到“可能很多”不等。

正如 Emmanuel 所说,从Fragments文档开始,然后在您有更详细的问题时随时返回 StackOverflow。

请注意,“固定标签 + 滑动”生成的代码只会在某些情况下显示标签:手机大小的屏幕纵向和平板电脑大小的屏幕横向。在其他配置中,选项卡将转换为下拉列表。如果这不是您想要的,请考虑PagerTabStrip在您的ViewPager.

于 2013-11-14T18:27:53.267 回答