public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
String[] tabTitles = new String[]{"热榜", "日榜"};
List<Fragment> fragmentList = new ArrayList<>();
fragmentList.add(new TestFragment());
fragmentList.add(new TestFragment());
ViewPageAdapter viewPageAdapter = new ViewPageAdapter(this, fragmentList);
binding.viewPager.setAdapter(viewPageAdapter);
binding.viewPager.setOffscreenPageLimit(1);
binding.viewPager.setUserInputEnabled(true);
binding.tabLayout.selectTab(binding.tabLayout.getTabAt(0));
new TabLayoutMediator(binding.tabLayout, binding.viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
View tabView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_layout_contact, null);
TextView tabIcon = tabView.findViewById(R.id.tv_tab_title);
View tabicon = tabView.findViewById(R.id.tabicon);
if (position == 0) {
tabIcon.setTextColor(Color.parseColor("#000000"));
tabIcon.setTypeface(null, Typeface.BOLD);
tabicon.setVisibility(View.VISIBLE);
} else {
tabIcon.setTextColor(Color.parseColor("#858585"));
tabIcon.setTypeface(null, Typeface.NORMAL);
tabicon.setVisibility(View.GONE);
}
tabIcon.setText(tabTitles[position]);
tab.setCustomView(tabView);
}
}).attach();
binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
binding.viewPager.setCurrentItem(tab.getPosition());
TextView tabTitleView = tab.getCustomView().findViewById(R.id.tv_tab_title);
View tabicon = tab.getCustomView().findViewById(R.id.tabicon);
tabTitleView.setTextColor(Color.parseColor("#000000"));
tabTitleView.setTypeface(null, Typeface.BOLD);
tabicon.setVisibility(View.VISIBLE);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
TextView tabTitleView = tab.getCustomView().findViewById(R.id.tv_tab_title);
View tabicon = tab.getCustomView().findViewById(R.id.tabicon);
tabTitleView.setTextColor(Color.parseColor("#858585"));
tabTitleView.setTypeface(null, Typeface.NORMAL);
tabicon.setVisibility(View.GONE);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="26dp"
android:orientation="vertical"
android:background="@color/white">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabIndicatorColor="#3BC7B5"
app:tabIndicator="@null"
app:tabIndicatorFullWidth="false"
android:background="#fff"
app:tabMinWidth="50dp" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
public class ViewPageAdapter extends FragmentStateAdapter {
private List<Fragment> list;
public ViewPageAdapter(@NonNull FragmentActivity fragmentActivity,
List<Fragment> fragmentList) {
super(fragmentActivity);
list = fragmentList;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return list.get(position);
}
@Override
public int getItemCount() {
return list.size();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/tabicon"
android:layout_width="40dp"
android:layout_height="6dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:background="#3BC7B5"
android:visibility="gone" />
<TextView
android:id="@+id/tv_tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="11111"
android:textSize="16sp" />
</RelativeLayout>