TabLayout+ViewPager实现标签卡效果
转载请注明原文地址:http://www.cnblogs.com/yanyojun/p/8082391.html
代码已经上传至Github:https://github.com/YanYoJun/ViewPagerDemo
先看效果

1、布局文件
<?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.plbear.yyj.myapplication.MainActivity"> <android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="40dp"
app:tabGravity="fill"
app:tabIndicatorColor="#4978ef"
app:tabIndicatorHeight="2dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="#4978ef"
app:tabTextColor="#222222"></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v4.view.ViewPager> </LinearLayout>
2、代码实现
package com.plbear.yyj.myapplication import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { var mFragList = ArrayList<Fragment>()
var adapter = object:FragmentPagerAdapter(supportFragmentManager){
override fun getItem(position: Int): Fragment {
return mFragList[position]
} override fun getCount(): Int {
return 2
}
} override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViewPager() //先将各个fragment加入到viewpager中
initTabLayout(); //初始化tablayout
} fun initTabLayout(){
tab.setupWithViewPager(view_pager)
tab.setTabsFromPagerAdapter(adapter)
tab.tabMode = TabLayout.MODE_FIXED
tab.getTabAt(0)?.setText("第一页")
tab.getTabAt(1)?.setText("第二页")
} fun initViewPager(){
mFragList.add(Fragment1())
mFragList.add(Fragment2()) view_pager.adapter = adapter
} }
这里需要注意点,需要先将setupWithViewPager和tablayout绑定起来,然后再初始化tab的标签页,这个非常重要,否则会造成tablayout中的文字显示不出来。
这里注意,要保证support包和design包的版本号一致,不然app会出现闪退
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile 'com.android.support:design:26.0.0-beta1'
compile 'com.android.support:support-v4:26.0.0-beta1'
implementation 'com.android.support:appcompat-v7:26.0.0-beta1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
}
TabLayout+ViewPager实现标签卡效果的更多相关文章
- ViewPagerWithRecyclerDemo【RecyclerView+ViewPager实现类似TabLayout+ViewPager效果】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用RecyclerView+ViewPager实现类似TabLayout+ViewPager效果. 效果图 使用步骤 一.项目组织 ...
- 011 Android TabLayout+ViewPager实现顶部滑动效果(多个页面)
1.TabLayout介绍 TabLayout提供了一个水平的布局用来展示Tabs,很多应用都有这样的设计,典型的有网易新闻,简书,知乎等.TabLayout就可以很好的完成这一职责,首先TabLay ...
- 使用FragmentTabHost+TabLayout+ViewPager实现双层嵌套Tab
大多数应用程序都会在底部使用3~5个Tab对应用程序的主要功能进行划分,对于一些信息量非常大的应用程序,还需要在每个Tab下继续划分子Tab对信息进行分类显示. 本文实现采用FragmentTabHo ...
- 浅谈TabLayout(ViewPager+Tab联动)
google发布了的Android Support Design库中提供了TabLayout 通过TabLayout+ViewPager实现导航栏效果,点击Tab ,ViewPager跟随变化,滑动V ...
- 介绍三个Android支持库控件:TabLayout+ViewPager+RecyclerView
本文主要介绍如下三个Android支持库控件的配合使用: TabLayout:android.support.design.widget.TabLayout ViewPager:android.sup ...
- [置顶]
xamarin Tablayout+Viewpager+Fragment顶部导航栏
最近几天不忙,所以把项目中的顶部导航栏的实现归集一下.android中使用TabLayout+ViewPager+Fragment制作顶部导航非常常见,代码实现也比较简单.当然我这个导航栏是基于xam ...
- Android开发之漫漫长途 Fragment番外篇——TabLayout+ViewPager+Fragment
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- 安卓TabLayout+ViewPager实现切页
安卓使用TabLayout+ViewPager+Fragment 实现页面切换,可实现左右滑动切换视图界面和点击切换 可自定义菜单栏是在顶部还是在底部 一.实现效果: 二.实现过程: 2.1 一些重要 ...
- FragmentTabHost实现标签卡效果
转载请注明原文链接:http://www.cnblogs.com/yanyojun/p/8099523.html 代码已上传到github:https://github.com/YanYoJun/Fr ...
随机推荐
- JQuery实现表格行的上移、下移、删除、增加
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%&g ...
- Flex+Java+Blazeds
1.环境:jdk1.6,Flex4.6 2.工具:MyEclipse10 3.server:Tomcat7 4.连接方式:Blazeds 5.项目类型:Flex项目 6.步骤 (1)新建Flex项目一 ...
- Buildroot构建指南——根文件系统(Rootfs)【转】
本文转载自; 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Buildroot构建指南——根文件系统(Rootfs) Buildroot的Rootfs构建流程有一个大 ...
- luogu4917天守阁的地板
https://www.zybuluo.com/ysner/note/1317548--- 题面 给出\(n\),用所有长为\(a\).宽为\(b\)\((1\leq a,b\leq n)\)的长方形 ...
- light oj 1205(数位DP)
题目描述: 求给定区间中的回文数有多少个? 首先明确一点,如果一个数是回文数,那么给这个数两边加上相同的数,那么这个数还是回文数. 根据这点就可以进行递推了,p[start][end]=9*p[sta ...
- JS获得本月的第一天和最后一天
<script> //本月第一天 function showFirstDay() { var Nowdate=new Date(); var MonthFirstD ...
- TS数据流PAT和PMT分析(转载)
转自:http://www.cnblogs.com/hjj801006/p/3837435.html TS流,是基于packet的位流格式,每个packet是188个字节或者204个字 节(一般是18 ...
- Android Debuggerd 简要介绍和源码分析(转载)
转载: http://dylangao.com/2014/05/16/android-debuggerd-%E7%AE%80%E8%A6%81%E4%BB%8B%E7%BB%8D%E5%92%8C%E ...
- Ubuntu12.04中新的快捷键(转载)
转自:http://blog.51osos.com/linuxnews/ubuntu12-04%E4%B8%AD%E6%96%B0%E7%9A%84%E5%BF%AB%E6%8D%B7%E9%94%A ...
- bzoj1048(记忆化搜索)
1048: [HAOI2007]分割矩阵 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1218 Solved: 890[Submit][Statu ...