简体   繁体   中英

How to start fragments after pressing on the action bar?

I made 2 different fragments and I want them to show in the 2 tabs. One fragment after pressing the first tab and the other fragment after pressing the second tab. After switching between the two tabs, only one fragment should be shown.

my first fragment names "fragment" and my second fragment is named "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));

When you are communicating with more then one fragments better use FragmentStatePagerAdapter, which will populate required fragment on corresponding tab selected.

First Create Toolbar,Tablayout,ViewPager in your xml file. Then Create Fragment and Fragment2.

Include this code in your 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) {

        }
    });

Create a PageAdapter like this...

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

Hope this will help you. Let me know if you need any further help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM