4

I'm trying out Android's new TabLayout class to add two tabs just below my ActionBar. Each tab will host a different fragment.

Also, I don't want to be able to swipe between my two tabs - to navigate between my tabs, I'd like to be able to ONLY touch the tab I want to navigate to.

Inside my MainActivity, I have:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Newsfeed"));
    tabLayout.addTab(tabLayout.newTab().setText("Random"));

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            // IDEALLY HERE, I'd like to do something like
            // tab.setFragment(new MainFragment()).
        }

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

        }

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

        }
    });
 }

So i'd like to override my onTabSelected and onTabReselected methods so that toggling between the two tabs leads to displaying two different fragments respectively. There's not much I could find online about the new TabLayout independent of the ViewPager.

ANy clues? Thanks!

user2635088
  • 1,598
  • 1
  • 24
  • 43

1 Answers1

8
@Override
public void onTabSelected(TabLayout.Tab tab) {
  Fragment f = heyWhatFragmentGoesInThisTab(tab);
  
  getFragmentManager()
    .beginTransaction()
    .replace(R.id.where_the_tab_contents_go, f)
    .commit();
}

where you need to write:

  • heyWhatFragmentGoesInThisTab() to return the Fragment that should be shown based upon the selected tab, and

  • R.id.where_the_tab_contents_go, which is a FrameLayout that serves as the container for the active fragment

IOW, you change fragments in response to the TabLayout the same way as you change fragments in response to action bar item clicks, nav drawer item clicks, or any other GUI event.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • This doesn't let the user swipe between fragments, because there is no ViewPager, How to add the Swipe Action and at the same time use the Fragment without ViewPager – Omar HossamEldin Oct 17 '16 at 12:42
  • 1
    @OmarHossam: I have no idea. – CommonsWare Oct 17 '16 at 12:55
  • This code is ***so badly formatted***! –  Jul 12 '23 at 13:46
  • @Ben: What specifically would you like changed, in those six lines of eight-year-old code? – CommonsWare Jul 12 '23 at 13:57
  • @CommonsWare Lots. It's not essential, but for readability, I'd say to put spaces around the equals sign on line 3. Also, splitting code between multiple lines like you have on lines 4 and 5 isn't great for readability. –  Jul 12 '23 at 15:36
  • Also introducing some local variables would be nice, such as one for the fragment manager, just to help split things up a bit. I'd downvote but don't want to loose the reputation. –  Jul 12 '23 at 15:44
  • 1
    @Ben: "I'd say to put spaces around the equals sign on line 3" -- done! "splitting code between multiple lines like you have on lines 4 and 5 isn't great for readability" -- I agree that the particular way I had that split was not great. I updated it to a more modern style. "introducing some local variables would be nice, such as one for the fragment manager, just to help split things up a bit" -- that would fail a code review, so I'll pass on that. – CommonsWare Jul 12 '23 at 22:08
  • @CommonsWare Thank you! Looks way better now. By the way, as a side note, this uses a *lot* of depreciated APIs. I understand this is an old question and you don't have to change anything, but just to let you know. –  Jul 12 '23 at 23:55
  • By the way, I recently posted [this question](https://stackoverflow.com/questions/76665707/tabitems-content-is-a-fragment) and feel its very similar to this post. It's not getting any attention, but I felt you would know what to do looking at your tag badges. –  Jul 13 '23 at 00:05