Tablayout basic usage, tablayout usage
Add dependency in gradle
Compile 'com. android. support: design: 26 +'
Layout File Code
MainActivity code. Note that the getPageTitle method must be rewritten in FragmentPagerAdapt;
Import android. support. design. widget. tabLayout; import android. support. v4.app. fragment; import android. support. v4.app. fragmentPagerAdapter; import android. support. v4.view. viewPager; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import java. util. arrayList; import java. util. list; public class MainActivity extends AppCompatActivity {private TabLayout tabLayout; private List
Titles = new ArrayList
(); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); tabLayout = (TabLayout) findViewById (R. id. tablayout); ViewPager viewPager = (ViewPager) findViewById (R. id. viewpager); initData (); viewPager. setAdapter (new FragmentPagerAdapter (getsuppfrfragmentmanager () {@ Override public Fragment getItem (int position) {return MyFragment. getinstance (titles. get (position) ;}@ Override public int getCount () {return titles. size () ;}@ Override public CharSequence getPageTitle (int position) {return titles. get (position) ;}}); tabLayout. setTabMode (TabLayout. MODE_SCROLLABLE); // display mode // associate viewpager with the top title in tabLayout. setupWithViewPager (viewPager);} private void initData () {for (int I = 0; I <10; I ++) {titles. add ("simulate" + I );}}}