第七天 TabLayout

Abina ·
更新时间:2024-11-13
· 674 次阅读

TabLayout public class MainActivity extends AppCompatActivity { private TabLayout tabId; private ViewPager vpId; private List list=new ArrayList(); private List titles=new ArrayList(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabId= findViewById(R.id.tab); vpId=findViewById(R.id.vp); list.add(new one()); list.add(new two()); list.add(new four()); titles.add("首页"); titles.add("第二页"); titles.add("第三页"); //适配器 MyFragmentAdapter myFragmentAdapter=new MyFragmentAdapter(getSupportFragmentManager(),list,titles); //关联 vpId.setAdapter(myFragmentAdapter); //把viewPager和tabLayout绑定在一起 tabId.setupWithViewPager(vpId); } class MyFragmentAdapter extends FragmentStatePagerAdapter { private List list; private List titles; public MyFragmentAdapter(@NonNull FragmentManager fm, List list, List titles) { super(fm); this.list = list; this.titles = titles; } @NonNull @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } //返回与viewpage关联以后的tablayot的内容 @Nullable @Override public CharSequence getPageTitle(int position) { return titles.get(position); } } }
作者:qq_46361858



tablayout

需要 登录 后方可回复, 如果你还没有账号请 注册新账号