TabLayout的高级使用
前言
前面介绍了TabLayout的基本属性和基本的使用方法。我是传送门。
真实的业务场景中,很多的效果,原生的TabLayout,并不支持。例如下滑线短于文字的效果,底部导航栏效果,标签文字选中是需要加粗效果等等。
所以我们需要使用TabLayout的自定义tab标签。
先上图。

先为急用的朋友上代码,后面做讲解
java类
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox; import java.util.ArrayList;
import java.util.List; import butterknife.BindView;
import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity {
@BindView(R.id.vp_all)
ViewPager vpAll;
@BindView(R.id.tab_custom)
TabLayout tabCustom; //存放自定义tab标签的列表
List<View> customTabList = new ArrayList<>();
//存放对应tab标签的fragment页面
List<PageFragment> fgList = new ArrayList<>();
//选中的自定义标签,标记器。默认选中第一个。标记位是0;
int indicator = 0; ViewPagerAdapter pagerAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);//此处是用的是butterKnife框架,等同于findviewbyid获取各个控件。
initTabCustomAndViewPager();
initListener();
} /**
* 初始化tab标签和viewpager内容。
*/
private void initTabCustomAndViewPager() {
//初始化10个自定义tab标签
for (int i = 0; i < 10; i++) {
final View tabView = getCustomTabView(this, "自定义" + i);
customTabList.add(tabView);
if (i==0){
//设置默认第一个选中效果
((CheckBox) tabView.findViewById(R.id.cb_name)).setChecked(true);
tabView.findViewById(R.id.cb_slide).setVisibility(View.VISIBLE);
}
tabCustom.addTab(tabCustom.newTab().setCustomView(tabView));
}
//初始化10个fragment页面
for (int i = 0; i < 10; i++) {
fgList.add(PageFragment.newInstance("我是内容栏目" + i));
}
pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fgList);
vpAll.setAdapter(pagerAdapter);
} /**
* 将viewpager和tabLayout之间的滑动事件和点击事件关联起来。
* 此处不能使用tabLayout的setupWithViewPager()方法,否则会造成自定义view失效
*/
private void initListener() {
//添加关联接口,此处不能用自带的绑定方法,否则自定义view会失效
vpAll.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
} @Override
public void onPageSelected(int i) {
tabCustom.getTabAt(i).select();
} @Override
public void onPageScrollStateChanged(int i) {
}
});
tabCustom.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//viewpager联动
vpAll.setCurrentItem(tab.getPosition());
//将之前选中的tab标签,设置为未选中状态
View oldView = customTabList.get(indicator);
CheckBox oldCbName = (CheckBox) oldView.findViewById(R.id.cb_name);
View oldCbSlide = oldView.findViewById(R.id.cb_slide);
oldCbName.setChecked(false);
oldCbSlide.setVisibility(View.INVISIBLE);
//当前选中的tab标签,设置为选中状态。
indicator = tab.getPosition();
View newView = customTabList.get(indicator);
CheckBox newCbName = (CheckBox) newView.findViewById(R.id.cb_name);
View newCbSlide = newView.findViewById(R.id.cb_slide);
newCbName.setChecked(true);
newCbSlide.setVisibility(View.VISIBLE);
} @Override
public void onTabUnselected(TabLayout.Tab tab) {
} @Override
public void onTabReselected(TabLayout.Tab tab) {
}
}); } /**
* ViewPager的适配器。
*/
class ViewPagerAdapter extends FragmentPagerAdapter { List<PageFragment> fragmentList; public ViewPagerAdapter(FragmentManager fm, List<PageFragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
} @Override
public Fragment getItem(int position) {
return fragmentList.get(position);
} @Override
public int getCount() {
return fragmentList.size();
}
} /**
* 获取自定义Tab标签的显示的内容
*
* @param context
* @param
* @return
*/
public static View getCustomTabView(Context context, String text) {
View view = LayoutInflater.from(context).inflate(R.layout.item_custom_tab, null);
CheckBox tabText = (CheckBox) view.findViewById(R.id.cb_name);
tabText.setText(text);
return view;
}
}
pageFragment的代码和布局就不贴了。不想写的朋友可以看上一篇:我是传送门
布局文件
activity_main的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.administrator.tablayoutdemo.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tab_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorHeight="0dp"
app:tabMode="scrollable"
/> <android.support.v4.view.ViewPager
android:id="@+id/vp_all"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </LinearLayout>
自定义tab标签的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical"
android:gravity="center"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="@+id/cb_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:text=""
android:textColor="@drawable/select_text_color"
android:textSize="15sp"
android:paddingTop="13dp"
android:ellipsize="end"
android:singleLine="true"
android:maxEms="5"
android:textStyle="bold"
android:paddingBottom="13dp"
android:clickable="false"
/>
<View
android:id="@+id/cb_slide"
android:layout_width="20dp"
android:layout_height="3dp"
android:visibility="invisible"
android:background="#ff4fbf86"/> </LinearLayout>
色值文件select_text_color,放在drawable目录下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#ff4fbf86" android:state_checked="true"/>
<item android:color="#BBBBBB" android:state_checked="false" />
</selector>
讲解:
OK上完代码,针对关键点在做一次说明
1、标签在初始化的过程中,需要对第一个tab标签提前进行选中状态的初始化,否则可能会造成第一次启动的时候,第一个标签没有出现选中状态。
2、viewpager和TabLayout标签进行联动的时候,不可以使用TabLayout的setupWithViewPager()方法,而是要通过ViewPager的addOnPageChangeListener()和Tablayout的addOnTabSelectedListener()方法进行两个控件之间的联动效果。否则会造成自定义的CustomeTab被TabLayout默认生成的标签覆盖掉。
3、在布局文件中,需要将TabLayout的tabIndicatorHeight设为0。用来屏蔽掉控件自动生成的下滑线。
通过自定义的Tab标签可以完全实现自己控制tab标签的内容,这里就不展示tab标签做为底部导航栏的效果了。原理都是一样的。
TabLayout的高级使用的更多相关文章
- TabLayout基本使用
前言 Tablayout继承自HorizontalScrollView,可以用作顶部标签效果.底部导航栏效果.一般多与ViewPager一起使用. 想直接了解如何实现短下滑效果的请看:TabLayou ...
- 重磅推出TabLayout高级窗口组件
TabLayout是在APICloud现有窗口系统基础上升级而来的高级窗口组件,符合Material Design规范,可通过简单的配置为窗口实现原生的导航栏和TabBar,它将帮助您节省30%以上的 ...
- 高级UI晋升之常用View(三)中篇
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从ViewPager来介绍常用View:文章目录 一.简介 二.基本使用 ...
- doT的高级用法及loadData的使用
本文出自APICloud官方论坛, 感谢论坛版主 gp3098的分享. 之前直接把模板写在页面底部的script标签内的,但是现在不同. 使用了doT.js配合api的loadData方法,整个页面就 ...
- MySQL高级知识- MySQL的架构介绍
[TOC] 1.MySQL 简介 概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,将数据保存在不同的表中,而 ...
- PayPal高级工程总监:读完这100篇论文 就能成大数据高手(附论文下载)
100 open source Big Data architecture papers for data professionals. 读完这100篇论文 就能成大数据高手 作者 白宁超 2016年 ...
- TabLayout + ViewPager
一.实现思路 1.在build.gradle中添加依赖,例如: compile 'com.android.support:support-v4:23.4.0'compile 'com.android. ...
- 马哥linux运维初级+中级+高级 视频教程 教学视频 全套下载(近50G)
马哥linux运维初级+中级+高级 视频教程 教学视频 全套下载(近50G)目录详情:18_02_ssl协议.openssl及创建私有CA18_03_OpenSSH服务及其相关应用09_01_磁盘及文 ...
- JS高级前端开发群加群说明及如何晋级
JS高级前端开发群加群说明 一.文章背景: 二. 高级群: 三. 加入方式: 四. 说明: 一.文章背景: 去年年初建了几个群,在不经意间火了,一直排在“前端开发”关键字搜索结果第一名.当然取得这 ...
随机推荐
- vim模式下报错E37: No write since last change (add ! to override)
故障现象: 使用vim修改文件报错,系统提示如下: E37: No write since last change (add ! to override) 故障原因: 文件为只读文件,无法修改. 解决 ...
- NAT(Network Address Translation)
一.概述 NAT英文全称是“Network Address Translation”,中文意思是“网络地址转换”,它是一个IETF(Internet Engineering Task Force, I ...
- android studio application应用打包jar
转载: https://blog.csdn.net/xiayiye5/article/details/79639044 首先我们来说下打成jar包的分类: 1.application应用打成jar包 ...
- canal mysql slave
[mysqld] log-bin=mysql-bin #添加这一行就ok binlog-format=ROW #选择row模式 server_id=1 #配置mysql replaction需要定义, ...
- java的智能提示无法打开
第一步:选中“window”->“preference” 第二步:选中“java”,并展开 第三步:选中“Editor”,并展开 第四步:选中“Content Assist”,在右侧 ...
- Xamarin.Forms 未能找到路径“x:\platforms”的一部分
https://stackoverflow.com/questions/45500269/xamarin-android-common-targets-error-could-not-find-a-p ...
- Python--Virtualenv简明教程(转载https://www.jianshu.com/p/08c657bd34f1)
virtualenv is a tool to create isolated Python environments. virtualenv通过创建独立Python开发环境的工具, 来解决依赖.版本 ...
- IT题库2-LinkList和ArrayList(插入数据、末尾插入数据、不同数据量插入数据)的效率?
ArrayList和LinkedList是两个集合类,用于存储一系列的对象引用(references).ArrayList是List接口的一个实现类,它的特点是查询效率高,增删效率低,线程不安全,原因 ...
- Qt3D 5.9 and future
2017-05 http://blog.qt.io/blog/2017/05/24/qt3d/ Qt3D future 5.9 Use Qt Quick or QPainter to render i ...
- datatable的select()结果直接修改,会体现在表上
如果你只是要修改DataTable里的DataRow的话没有必要找到它的行号,直接在Select后得到的DataRow上修改就行了,它的修改会直接反映到DataTable上(其实就是直接修改了Data ...