-1

I used switch for creating tabbed and Fragment to change pages by tabbed on. now I want to have statement to say if user clicks one for example position 0 like chats Fragment my toolbar changes .

what i don't know is that how can i put an if statement on my result class SectionsPagerAdapter tabbed to change my toolbars that i created before. I did some if statement on my mViewPager or mTabLayout but didn't work.

My Activity for SectionsPagerAdapter :

     class SectionsPagerAdapter extends FragmentPagerAdapter{
            public SectionsPagerAdapter(FragmentManager fm) {
                super( fm );
            }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    ChatsFragment chatsFragment = new ChatsFragment();
                    return chatsFragment;

                case 1:
                    ViewFragment viewFragment = new ViewFragment();
                    return viewFragment;

                case 2:
                    AccountFragment accountFragment = new AccountFragment();
                    return accountFragment;


                    default:
                        return null;



            }
        }
   @Override
    public int getCount() {
        return 3;
    }

    public CharSequence getPageTitle(int position){
        switch (position){

            case 0:
                return "Chats";

            case 1:
                return "View";

            case 2:
                return "Account";

            default:
                    return null;

        }

    }
}

My Code in Mainpage:

mViewPager=(ViewPager)findViewById( R.id.main_tabPager );
mSectionPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager() );
        mViewPager.setAdapter( mSectionPagerAdapter );
        mTabLayout =(TabLayout) findViewById( R.id.main_tabs );
        mTabLayout.setupWithViewPager( mViewPager );
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
vivanred
  • 13
  • 5

1 Answers1

0

I solve my Problem .

   mTabLayoutListener.addOnTabSelectedListener( new TabLayout.OnTabSelectedListener() {


     @Override
     public void onTabSelected(TabLayout.Tab tab) {
      int position = tab.getPosition();
       if (position ==0){
        //toolbar
             mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
             setSupportActionBar(mToolbar);
             getSupportActionBar().setTitle("ViV");
 }
 else   if (position ==1){
 //toolbar
         mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
         setSupportActionBar(mToolbar);
         getSupportActionBar().setTitle("Vi");
     }
else if (position ==2){
  //toolbar
    mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
   setSupportActionBar(mToolbar);
   getSupportActionBar().setTitle("V");
   }

  }

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

 }

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

       }


});
vivanred
  • 13
  • 5