content: package com.androidkun.xtablayout;
  
  import android.os.Bundle;
  import android.support.design.widget.TabLayout;
  import android.support.v4.app.Fragment;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class MainActivity extends AppCompatActivity {
  
      List<Fragment> fragments = new ArrayList<>();
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          initViewPager();
      }
  
      private void initViewPager() {
          List<String> titles = new ArrayList<>();
          titles.add("一本");
          titles.add("二本");
          titles.add("三A");
          titles.add("三B");
          titles.add("一本");
          titles.add("二本");
          titles.add("三A");
          titles.add("三B");
          for (int i = 0; i < titles.size(); i++) {
              if(i%2==0){
                  fragments.add(new Fragment2());
              }else{
                  fragments.add(new Fragment1());
              }
          }
          FragmentAdapter adatper = new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
          ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
          viewPager.setAdapter(adatper);
          viewPager.setOffscreenPageLimit(4);
          //将TabLayout和ViewPager关联起来。
          final XTabLayout tabLayout = (XTabLayout) findViewById(R.id.xTablayout);
          tabLayout.setupWithViewPager(viewPager);
          //给TabLayout设置适配器
          tabLayout.setupWithViewPager(viewPager);
  
      }
  }
  