一、主Activity
public class MainActivity extends AppCompatActivity { TabLayout tabLayout; ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUiAndListeners(); } private void initUiAndListeners(){ tabLayout=(TabLayout)findViewById(R.id.tab_layout); viewPager=(ViewPager)findViewById(R.id.view_pager); List<String> titles = new ArrayList<>(); titles.add("1"); titles.add("2"); titles.add("3"); List<Fragment> fragments = new ArrayList<>(); fragments.add(TestFragment.newInstance(1)); fragments.add(TestFragment.newInstance(2)); fragments.add(TestFragment.newInstance(3)); initViewPagerAdapter(fragments,titles); } public void initViewPagerAdapter(List<Fragment> fragments, List<String> titles) { SimpleFragmentAdapter adapter = new SimpleFragmentAdapter(getSupportFragmentManager(), fragments, titles); viewPager.setOffscreenPageLi