📅  最后修改于: 2023-12-03 14:39:09.170000             🧑  作者: Mango
在Android中,TabLayout是一种常见的布局类型,用于在不同的选项卡之间切换,并且它非常易于使用。TabLayout使用ViewPager来管理选项卡的内容视图。在编程中,有时需要以编程方式设置选定的选项卡,这篇文章将给出具体实现步骤。
在布局文件中添加TabLayout和ViewPager:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="center"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="@id/tab_layout"/>
</androidx.constraintlayout.widget.ConstraintLayout>
在Activity或Fragment中的代码实现中,首先需要获取TabLayout和ViewPager:
TabLayout tabLayout = findViewById(R.id.tab_layout);
ViewPager viewPager = findViewById(R.id.view_pager);
接下来,创建一个PagerAdapter来管理ViewPager的内容视图:
private void setupViewPager(ViewPager viewPager) {
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentA(), "Fragment A");
adapter.addFragment(new FragmentB(), "Fragment B");
adapter.addFragment(new FragmentC(), "Fragment C");
viewPager.setAdapter(adapter);
}
在MyPagerAdapter中,需要实现以下方法:
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentTitleList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
接下来,使用TabLayout和ViewPager联合起来。使用TabLayout的setupWithViewPager方法指定ViewPager,将TabLayout和ViewPager关联起来:
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
最后,调用TabLayout的setSelectedTabIndicatorColor方法设置选中的选项卡颜色:
TabLayout.Tab tab = tabLayout.getTabAt(2);
tab.select();
tabLayout.setSelectedTabIndicatorColor(Color.BLUE);
完整的实例代码如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = findViewById(R.id.tab_layout);
ViewPager viewPager = findViewById(R.id.view_pager);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
TabLayout.Tab tab = tabLayout.getTabAt(2);
tab.select();
tabLayout.setSelectedTabIndicatorColor(Color.BLUE);
}
private void setupViewPager(ViewPager viewPager) {
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentA(), "Fragment A");
adapter.addFragment(new FragmentB(), "Fragment B");
adapter.addFragment(new FragmentC(), "Fragment C");
viewPager.setAdapter(adapter);
}
private static class MyPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public MyPagerAdapter(@NonNull FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentTitleList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
}
}
在本文中,介绍了如何以编程方式设置选定的选项卡。使用TabLayout和ViewPager,可以轻松地在应用程序中添加选项卡布局,并以编程方式操作选项卡。