content: package com.alelak.materialupsample;
  
  import android.os.Bundle;
  import android.support.design.widget.TabLayout;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class MainActivity extends AppCompatActivity {
      private static final String TAG = MainActivity.class.getName();
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          setupToolbar();
          setupViewPager();
      }
  
      private void setupToolbar() {
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
      }
  
      private void setupViewPager() {
          final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
          final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
          final Adapter adapter = new Adapter(getSupportFragmentManager());
          adapter.addFragment(new PopularPostsFragment(), "Popular");
          adapter.addFragment(new LatestPostsFragment(), "Latest");
          viewPager.setAdapter(adapter);
          tabLayout.setupWithViewPager(viewPager);
      }
  
      static class Adapter extends FragmentPagerAdapter {
          private final List<Fragment> mFragments = new ArrayList<>();
          private final List<String> mFragmentTitles = new ArrayList<>();
  
          public Adapter(FragmentManager fm) {
              super(fm);
          }
  
          public void addFragment(Fragment fragment, String title) {
              mFragments.add(fragment);
              mFragmentTitles.add(title);
          }
  
          @Override
          public Fragment getItem(int position) {
              return mFragments.get(position);
          }
  
          @Override
          public int getCount() {
              return mFragments.size();
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              return mFragmentTitles.get(position);
          }
  
      }
  }
  