转载请注明原文地址: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实现标签卡效果的更多相关文章

  1. ViewPagerWithRecyclerDemo【RecyclerView+ViewPager实现类似TabLayout+ViewPager效果】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用RecyclerView+ViewPager实现类似TabLayout+ViewPager效果. 效果图 使用步骤 一.项目组织 ...

  2. 011 Android TabLayout+ViewPager实现顶部滑动效果(多个页面)

    1.TabLayout介绍 TabLayout提供了一个水平的布局用来展示Tabs,很多应用都有这样的设计,典型的有网易新闻,简书,知乎等.TabLayout就可以很好的完成这一职责,首先TabLay ...

  3. 使用FragmentTabHost+TabLayout+ViewPager实现双层嵌套Tab

    大多数应用程序都会在底部使用3~5个Tab对应用程序的主要功能进行划分,对于一些信息量非常大的应用程序,还需要在每个Tab下继续划分子Tab对信息进行分类显示. 本文实现采用FragmentTabHo ...

  4. 浅谈TabLayout(ViewPager+Tab联动)

    google发布了的Android Support Design库中提供了TabLayout 通过TabLayout+ViewPager实现导航栏效果,点击Tab ,ViewPager跟随变化,滑动V ...

  5. 介绍三个Android支持库控件:TabLayout+ViewPager+RecyclerView

    本文主要介绍如下三个Android支持库控件的配合使用: TabLayout:android.support.design.widget.TabLayout ViewPager:android.sup ...

  6. [置顶] xamarin Tablayout+Viewpager+Fragment顶部导航栏

    最近几天不忙,所以把项目中的顶部导航栏的实现归集一下.android中使用TabLayout+ViewPager+Fragment制作顶部导航非常常见,代码实现也比较简单.当然我这个导航栏是基于xam ...

  7. Android开发之漫漫长途 Fragment番外篇——TabLayout+ViewPager+Fragment

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  8. 安卓TabLayout+ViewPager实现切页

    安卓使用TabLayout+ViewPager+Fragment 实现页面切换,可实现左右滑动切换视图界面和点击切换 可自定义菜单栏是在顶部还是在底部 一.实现效果: 二.实现过程: 2.1 一些重要 ...

  9. FragmentTabHost实现标签卡效果

    转载请注明原文链接:http://www.cnblogs.com/yanyojun/p/8099523.html 代码已上传到github:https://github.com/YanYoJun/Fr ...

随机推荐

  1. 我的package.json清单

    { "name": "lists", "version": "1.0.0", "main": &qu ...

  2. DLR之 ExpandoObject和DynamicObject的使用演示样例

    ExpandoObject :动态的增删一个对象的属性,在低层库(比如ORM)中非常实用.因为ExpandoObject实现了IDictionay<string, object>接口,常见 ...

  3. seajs载入流程图

    近期读seajs源代码,整理出了主要逻辑的流程图(注意:是逻辑示意图).感兴趣的朋友能够看看,欢迎批评指正~ http://www.gliffy.com/go/publish/image/607216 ...

  4. SpringMVC_基本配置 --跟海涛学SpringMVC(和自己在项目中的实际使用的对比)

    ☆依赖jar包: 1.Spring框架jar 包: 为了简单,将spring-framework-3.1.1.RELEASE-with-docs.zip/dist/下的所有jar包拷贝到项目的WEB- ...

  5. Mac Mysql [ERR] 2006 - MySQL server has gone away

    Mac mysql 安装后,导入sql数据,出现这个错误: 处理方式,是因为sql文件太大,需要修改mysql的配置.如果没有my.cnf就自己建一个. cd /etc sudo vim my.cnf ...

  6. POSTMAN模拟数组数据

    有时候写接口,需要传入数据数据.比如购物车中的一组商品.它们的数量是不固定的,只能用数组才能更好的处理. 怎么用POSTMAN模拟呢? 万能的POSTMAN.

  7. mysql查询表的字符集

    mysql查询表的字符集 SHOW CREATE TABLE user;

  8. 风云流水 jQuery技巧总结 (转)

    jQuery技巧总结 (转) 一.简介 1.1.概述 随着WEB2.0及ajax思想在互联网上的快速发展传播,陆续出现了一些优秀的Js框架,其中比较著名的有Prototype.YUI.jQuery.m ...

  9. ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling

    ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling ...

  10. Java:目录

    ylbtech-Java:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtech. ...