Android开发 Tablayout的学习

前言

  Tablayout通常作主页底下的导航栏开发或者上面的选择栏开发,就我的感受Tablayout用于主页导航栏会比BottomNavigationView更好,自定义方面也更容易.缺点是没有动画也不是Material Design设计模式的一部分.下面就讲解用于有导航栏的主页开发:android

  通常主页导航栏与内容用Tablayout与Fragment配合使用设计模式

  1.第一种Tablayout+ViewPager+Fragment,好处是能够左右滑动不须要本身实现滑动,而且能够有动画出现app

  2.第二种Tablayout+Fragment,若是你不须要左右滑动就能够轻松的选择这个模式.ide

在xml里

  由于有2种添加Item的方式,因此写法也能够是2个种布局

  第一种,这种方法能够直接配置Item的名称属性动画

<com.google.android.material.tabs.TabLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        
    </com.google.android.material.tabs.TabLayout>

第二种,这种须要本身在代码里添加子Item,而且能够实现自定义布局和View的Item(下面会说明在代码里添加Item)this

<com.google.android.material.tabs.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="0dp"
        android:layout_height="56dp"
        app:tabIndicatorHeight="0dp"
        app:tabBackground="@android:color/transparent"
        app:tabRippleColor="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">
    </com.google.android.material.tabs.TabLayout>

介绍一些关键的xml属性

若是你不须要选中下划线,能够使用这个属性取消google

app:tabIndicatorHeight="0dp"

若是你不须要点击后的阴影加涟漪动画效果,能够使用下面2个属性取消spa

app:tabBackground="@android:color/transparent"
app:tabRippleColor="@android:color/transparent"

代码部分讲解

若是你须要与ViewPager配合使用,那么你须要知道下面的关键代码

ContentFragment fragment1 = new ContentFragment(); 
ContentFragment fragment2 = new ContentFragment(); 
ContentFragment fragment3 = new ContentFragment(); 
ContentFragment fragment4 = new ContentFragment(); fragments.add(fragment1); fragments.add(fragment2); fragments.add(fragment3); fragments.add(fragment4); 
adapter = new FragmentAdapter(getSupportFragmentManager(),fragments); 
viewpager.setAdapter(adapter); tablayout.setupWithViewPager(viewpager);
tablayout.setupWithViewPager(viewpager);是关键,负责链接ViewPager与Tablayout,固然选择的适配器FragmentAdapter也是关键

若是你是须要自定义Item(下面用建立Tab的方式添加到TabLayout里,就前面说的代码里添加Item)

private void addTabData() {
   mTablayout = findViewById(R.id.tablayout);
        String[] tabContentArray = {"首页", "通知", "圈子", "个人"};
        int[] tabIconArray = {R.drawable.ic_home_homepage, R.drawable.ic_home_notice, R.drawable.ic_home_circle, R.drawable.ic_home_my};
        for (int i = 0; i < 4; i++) {
            TabLayout.Tab tab = mTablayout.newTab();//关键的建立一个Tab,注意这里使用的是已经实例的mTablayout建立的Tab,很容易疏忽使用new Tablayout().new Tab()的方式建立,这个是会报错的.
            View view = LayoutInflater.from(this).inflate(R.layout.home_tab_item,mTablayout,false);
            ImageView icon = view.findViewById(R.id.icon);
            TextView content = view.findViewById(R.id.content);
            icon.setImageResource(tabIconArray[i]);
            content.setText(tabContentArray[i]);
            tab.setCustomView(view);
            if (i == 0){
                mTablayout.addTab(tab,0,true);//设置选择的item
            }else {
                mTablayout.addTab(tab);
            }
        }
    }
home_tab_item.xml
<?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"
    android:orientation="vertical"
    android:layout_width="35dp"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/red_dot"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:background="@drawable/bg_reddot"
        app:layout_constraintTop_toTopOf="@id/icon"
        app:layout_constraintLeft_toRightOf="@id/icon"
        app:layout_constraintRight_toRightOf="@id/icon"/>

    <ImageView
        android:id="@+id/icon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <TextView
        android:id="@+id/content"
        android:textColor="@color/fontColorMain2"
        android:textSize="@dimen/font_size_11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        app:layout_constraintTop_toBottomOf="@id/icon"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

ic_home_homepage.xml设计

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@mipmap/ic_home_page_select"/>
    <item android:state_selected="false" android:drawable="@mipmap/ic_home_page"/>

</selector>

实现图片的选中变化

Tablayout选中回调

private void initTablayoutListener(){
        mTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //选中某个tab

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //当tab从选择到未选择

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //已经选中tab后的重复点击tab

            }
        });
    }

 

 

end

相关文章
相关标签/搜索