com.google.android.material.tabs.TabLayout

news/2024/5/17 20:08:27 标签: android, gitee

一、布局

<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    tools:context=".main.MainActivity">


    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/main_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="65dp"/>

    <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout_main"
            android:layout_width="match_parent"
            android:background="@color/white"
            app:tabGravity="fill"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed"
            android:layout_height="65dp" />

</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/transparent"
    android:gravity="center">

    <CheckBox
        android:id="@+id/tb_main_tab_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@null"
        android:checked="false"
        android:focusable="false"
        android:clickable="false"
        android:text="" />

    <TextView
        android:id="@+id/tv_main_tab_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dimen_4"
        android:text=""
        android:textSize="@dimen/font_12"
        android:clickable="false"
        android:textColor="@drawable/common_tab_text_color" />

</LinearLayout>

 

二、界面中使用

private String[] tabs;
private int[] resIds;
  tabs = this.getResources().getStringArray(R.array.main_tab_btn_name);
        resIds = new int[]{R.drawable.icon_main_tab_bg,
                R.drawable.icon_project_tab_bg,
                R.drawable.icon_block_tab_bg,
                R.drawable.icon_me_tab_bg};

        ViewPagerScrollAdapter scrollAdapter = new ViewPagerScrollAdapter(getSupportFragmentManager(), getLifecycle(), fragmentList);

        binding.mainViewpager.setAdapter(scrollAdapter);

        binding.mainViewpager.setUserInputEnabled(false);
//        binding.mainViewpager.setOffscreenPageLimit(2);
        binding.tabLayoutMain.setTabTextColors(R.color.color_3D80FC, R.color.color_3D80FC);

        mediator = new TabLayoutMediator(binding.tabLayoutMain, binding.mainViewpager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //自定义TabView
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_main_tab_view, null);
                view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1));

                TextView tabView = view.findViewById(R.id.tv_main_tab_text);
                tabView.setText(tabs[position]);


                CheckBox checkBox = view.findViewById(R.id.tb_main_tab_checkbox);
                checkBox.setBackgroundResource(resIds[position]);
                if (position == 0) {
                    checkBox.setChecked(true);
                    tabView.setSelected(true);
                }

                tab.setCustomView(view);
            }
        });

        binding.tabLayoutMain.setSelectedTabIndicatorHeight(0); //去掉下划线
        binding.tabLayoutMain.setTabRippleColor(ColorStateList.valueOf(getContext().getResources().getColor(R.color.white)));//去掉黑色背景

        //要执行这一句才是真正将两者绑定起来
        mediator.attach();
    binding.tabLayoutMain.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                selectOrLogin();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ((CheckBox) tab.getCustomView().findViewById(R.id.tb_main_tab_checkbox)).setChecked(false);
                tab.getCustomView().findViewById(R.id.tv_main_tab_text).setSelected(false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                selectOrLogin();
            }
        });
 
ViewPagerScrollAdapter 
public class ViewPagerScrollAdapter extends FragmentStateAdapter  {
    private ArrayList<Fragment> fragmentList;

    public ViewPagerScrollAdapter(@NonNull Fragment fragment) {
        super(fragment);
    }

    public ViewPagerScrollAdapter(@NonNull FragmentActivity fragmentActivity,ArrayList<Fragment> fragmentList) {
        super(fragmentActivity);
        this.fragmentList=fragmentList;
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        recyclerView.setItemViewCacheSize(fragmentList.size());
    }

    public ViewPagerScrollAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle,ArrayList<Fragment> fragmentList) {
        super(fragmentManager, lifecycle);
        this.fragmentList=fragmentList;
    }


    @NonNull
    @Override
    public Fragment createFragment(int position) {
        // 返回Fragment
        return fragmentList.get(position);
    }

    @Override
    public int getItemCount() {
        // 获取Fragment数量
        return fragmentList.size();
    }

}


http://www.niftyadmin.cn/n/5383445.html

相关文章

MySQL的数据类型和细节

1.整型 数值类型字节描述TINYINT[UNSIGNED]1很小的整数&#xff0c;默认有符号 [-128,127]/[0,255]SMALLINT[UNSIGNED]2较小的整数&#xff0c;默认有符号 [-32768,32767]/[0,65535]MEDIUMINT[UNSIGNED]3中等的整数&#xff0c;默认有符号 [-8388608,8388607]/[0,16777215]…

挑战杯 基于Django与深度学习的股票预测系统

文章目录 0 前言1 课题背景2 实现效果3 Django框架4 数据整理5 模型准备和训练6 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; **基于Django与深度学习的股票预测系统 ** 该项目较为新颖&#xff0c;适合作为竞赛课题方向&#xff…

读写分离的利器——MySQL Proxy

0 引言 MySQL Proxy是一个位于客户端和MySQL服务器端之间的程序&#xff0c;通过它可以实现监听和管理客户端与MySQL服务器端之间的通信&#xff0c;最大的作用是实现数据库的读写分离&#xff0c;从而达到负载均衡的目的。 MySQL Proxy的常用用途包括负载平衡、故障分析、查…

提高供应商收发文件效率的同时,如何保障数据的安全流转?

数据文件是制造业企业的核心竞争力&#xff0c;一旦发生数据外泄&#xff0c;就会给企业造成经济损失。之前就出现过像小米二级供应商因对其下游供应商管理不善&#xff0c;泄露了小米汽车前后保险杠的早期设计稿事件。制造业企业与供应商之间业务联系紧密&#xff0c;文件流转…

用Dockerfile创建PostgreSQL数据库

官网下载基本镜像 [rootlocalhost ~]# docker pull postgres:latest创建项目目录 [rootlocalhost ~]# mkdir /root/postgresql创建一个名为 “Dockerfile” 的文件 [rootlocalhost ~]# vim Dockerfile # 使用官方的PostgreSQL镜像 FROM postgres:latest# 设置环境变量&#…

文件包含+文件上传漏洞(图片马绕过)

目录 一.文件包含二.文件上传三.图片马四.题目 一.文件包含 将已有的代码以文件形式包含到某个指定的代码中&#xff0c;从而使用其中的代码或者数据&#xff0c;一般是为了方便直接调用所需文件&#xff0c;文件包含的存在使得开发变得更加灵活和方便&#xff08;若对用户输入…

SpringCloud--Ribbon解析

一、Spring Cloud Ribbon简介 Spring Cloud Ribbon是Spring Cloud生态系统中的一部分&#xff0c;是一套基于 Netflix Ribbon 实现的客户端负载均衡工具&#xff0c;由于Spring Cloud对其进行二次封装&#xff0c;可以将面向服务的Rest模板&#xff08;RestTemplate&#xff0…

rocketMq-broker-PullMessageProcessor 的完整生命周期和设计思想

前面我们说到&#xff0c;根据消息拉取命令RequestCode.PULL_MESSAGE,可以找到broker端处理消息拉取的入口&#xff1a;org.apache.rocketmq.broker.processor.PullMessageProcessor #processRequest&#xff0c;在processRequest中执行消息拉取。 很多文章直接讲了processRequ…