TabLayout 和 ViewPager2

效果图

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一起学鸿蒙呀~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值