按下操作栏后如何启动片段?

How to start fragments after pressing on the action bar?

我制作了 2 个不同的片段,我希望它们显示在 2 个选项卡中。按下第一个选项卡后的一个片段和按下第二个选项卡后的另一个片段。在两个选项卡之间切换后,应该只显示一个片段。

我的第一个片段命名为 "fragment",我的第二个片段命名为 "fragment2"

//Hallo ich hätte gerne tabs
            final ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);



            //tab listener
            ActionBar.TabListener tabListener1 = new ActionBar.TabListener(){

                @Override
                public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                }

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

                }

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

                }
            };


            // für den zweiten tab
            ActionBar.TabListener tabListener2 = new ActionBar.TabListener(){

                @Override
                public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                }

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

                }

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

                }
            };



            //Tabs erstellen
            actionBar.addTab(actionBar.newTab().setText("Tab Nummer Eins").setTabListener(tabListener1));
            actionBar.addTab(actionBar.newTab().setText("Tab Nummer Zwei").setTabListener(tabListener2));

当您与多个片段通信时,最好使用 FragmentStatePagerAdapter,它将在所选的相应选项卡上填充所需的片段。

首先在 xml 文件中创建工具栏、Tablayout、ViewPager。 然后创建Fragment和Fragment2.

将此代码添加到您的 HomeActivity.class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.list_home));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.save));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PageAdapter adapter = new PageAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            Log.i("tag1","on tab");
            viewPager.setCurrentItem(tab.getPosition());// passing selected tab position to adapter and getting reuqired fragment
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

像这样创建一个 PageAdapter...

public class PageAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PageAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);

    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            Fragment tab1 = new Fragment(); //When click First tab 
            return tab1;
        case 1:
            Fragment2 tab2 = new Fragment2(); //When click on second tab
            return tab2;
        default:
            return null;
    }
}

@Override
public int getCount() {

    Log.i("tag1","on getcount");
    return mNumOfTabs;
                    }
}

希望对您有所帮助。如果您需要任何进一步的帮助,请告诉我。