-1

「固定タブ+スワイプ」のナビゲーションを使っていて、アクティビティの内容とロジックを各タブに設定したい。私はたくさん検索してきましたが、解決策は見つかりませんでした。

私は次のものを持っています:

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

各タブに表示するために、アクティビティをフラグメントに「変換」するにはどうすればよいですか? 助けてください!

4

1 に答える 1

1

アクティビティをフラグメントに変換する普遍的なレシピはありません。一般的に言えば、onCreate()ロジックはメソッドに変換される傾向があり、onCreateView()( call ではなくsetContentView()) UI を返します。他のライフサイクル メソッド (例: onPause()) はそのまま機能するはずです。残りのアクティビティにどの程度の変更があるかは、「あまりない」から「おそらく多い」までさまざまです。

Emmanuel が指摘しているように、フラグメントのドキュメントから始めて、より詳細な質問がある場合は StackOverflow に戻ってください。

「固定タブ + スワイプ」生成コードは、特定の状況でのみタブを表示することに注意してください: 縦向きの電話サイズの画面と横向きのタブレット サイズの画面。他の構成では、タブはドロップダウン リストに変換されます。これが望ましくない場合はPagerTabStrip、アクション バー タブの代わりに を使用することを検討してくださいViewPager

于 2013-11-14T18:27:53.267 に答える