自定义tablayout实现

tablayout 在布局文件中展示
<com.google.android.material.tabs.TabLayout
                android:id="@+id/tab_matter_detail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ll_detail"
                android:layout_marginTop="@dimen/margin_space_10"
                app:tabBackground="@color/white"
                app:tabIndicatorColor="@color/appThemeColor"
                app:tabIndicatorHeight="2dp"
                app:tabSelectedTextColor="@color/appThemeColor"
                app:tabTextColor="#66000000" />
tablayout 初始化及切换
/**
     * 切换卡 tablayout
     */
    String[] tabArray = {"工作沟通", "反馈情况"};
    private void initTab() {

        for (int i = 0; i < 2; i++) {
            TabLayout.Tab tab = tabMatterDetail.newTab();
            //注意!!!这里就是添加我们自定义的布局
            tab.setCustomView(getCustomView(i));
            //这里是初始化时,默认item0被选中,setSelected(true)是为了给图片和文字设置选中效果,代码在文章最后贴出
            if (i == 0) {
                (tab.getCustomView().findViewById(R.id.tab_iv)).setSelected(true);
                ( tab.getCustomView().findViewById(R.id.tv_title)).setSelected(true);
            }
            tabMatterDetail.addTab(tab);
        }
        if("1".equals(type)){
            initSelect(0);
        }else{
            initSelect(1);
        }
        tabMatterDetail.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
              
                if (tab.getPosition() == 0) {
                   //切换逻辑
                    type="1";
                } else {
                  //切换逻辑  
                    type="2";
                }
                (tab.getCustomView().findViewById(R.id.tab_iv)).setSelected(true);
                (tab.getCustomView().findViewById(R.id.tv_title)).setSelected(true);
                //全部逻辑
                、、、
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                (tab.getCustomView().findViewById(R.id.tab_iv)).setSelected(false);
                (tab.getCustomView().findViewById(R.id.tv_title)).setSelected(false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
注意!!!这里就是我们自定义的布局tab_item
    public View getCustomView(int position){
        View view= LayoutInflater.from(this).inflate(R.layout.layout_common_tablayout_style1,null);
        ImageView iv= view.findViewById(R.id.tab_iv);
        TextView tv=view.findViewById(R.id.tv_title);
        switch (position){
            case 0:
                iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_matter_detail_gzgt));
                tv.setText(tabArray[0]);
                tvTitleArray[0]=tv;
                imageViewArray[0]=iv;
                break;
            case 1:
                iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_matter_detail_fkqk));
                tv.setText(tabArray[1]);
                tvTitleArray[1]=tv;
                imageViewArray[1]=iv;
                break;

        }
        return view;
    }
    TextView tvTitleArray[]=new TextView[2];
    ImageView imageViewArray[]=new ImageView[2];
    private void initSelect(int i){
        tvTitleArray[i].setSelected(true);
        imageViewArray[i].setSelected(true);
    }
layout_common_tablayout_style1布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    >

    <ImageView
        android:id="@+id/tab_iv"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:src="@drawable/tab_group_sel"
        >
    </ImageView>

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="人员"
        android:textSize="@dimen/textsize_16"
        android:textColor="@color/tablayout_common_tv_bg"
        android:layout_marginLeft="10dip"
        >

    </TextView>



</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值