• TabLayout的xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!-- TODO: Update blank fragment layout -->
<!-- Toolbar -->
<LinearLayout
android:id="@+id/daily_pic"
android:layout_width="match_parent"
android:layout_height="60dp"
android:paddingTop="16dp"
android:paddingLeft="18dp"
android:background="@color/blue_btn_color_normal"> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <ImageButton
android:id="@+id/btn_wei_left1"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginTop="4dp"
android:background="@drawable/ic_me_left"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="优惠券"
android:paddingRight="50dp"
android:layout_marginLeft="115dp"
android:gravity="left"
android:textSize="21dp"
android:textColor="#f7efef"
android:background="@color/blue_btn_color_normal"/>
</LinearLayout>
</LinearLayout> <android.support.design.widget.TabLayout
android:id="@+id/tab_layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
>
</android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
/> </LinearLayout>
  • ViewPager里面的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂无"
android:layout_gravity="center"
android:layout_marginLeft="150dp"
android:textSize="50dp"
/> </LinearLayout>
  • 具体实现代码,创建Fragment
package com.example.accessHand2.Home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import com.example.accessHand2.R; /**
* Created by Douzi on 2017/7/15.
*/ public class PageFragment extends Fragment { public static final String ARGS_PAGE = "args_page";
private int mPage; public static PageFragment newInstance(int page) {
Bundle args = new Bundle(); args.putInt(ARGS_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARGS_PAGE);
} @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.youhui_fragment,container,false); TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText("第" + mPage + "页"); //设置ViewPager内容
textView.setTextSize(20); return view;
}
}
  • 创建适配器
package com.example.accessHand2.Home;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; /**
* Created by Douzi on 2017/7/15.
*/ public class MyFragmentPagerAdapter extends FragmentPagerAdapter { public final int COUNT = 2;
private String[] titles = new String[]{"可用(0)", "暂无可用(0)"}; //设置tab的标题
private Context context; public MyFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
} @Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
} @Override
public int getCount() {
return COUNT;
} @Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
  • Fragment+ViewPager+FragmentPagerAdapter的组合
     mTabLayout = (TabLayout) findViewById(R.id.tab_layout2);
//添加 ViewPager内容
     ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),
this);
viewPager.setAdapter(adapter); //将 viewPager 和 tab 关联到一起, 很重要 !
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
       mTabLayout.setupWithViewPager(viewPager);
  • TabLayout的使用 (方法一,手动添加)
   TabLayout tabLayout = ...;
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
  • TabLayout的使用 (方法二,自动添加, 就是上面那个组合的代码)
     mTabLayout = (Tabayout) findViewById(R.id.tab_layout2);
//添加 ViewPager内容
     ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),
this);
viewPager.setAdapter(adapter); //将 viewPager 和 tab 关联到一起, 很重要 !
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
     mTabLayout.setupWithViewPager(viewPager);
     mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);     //使Tab填满 屏幕宽度
     mTabLayout.setTabMode(TabLayout.MODE_FIXED);

笔记(二)TabLayout + ViewPager + FragmentPagerAdapter 组合用法的更多相关文章

  1. [读书笔记]C#学习笔记二: 委托和事件的用法及不同.

    前言:  C#委托是什么 c#中的委托可以理解为函数的一个包装, 它使得C#中的函数可以作为参数来被传递, 这在作用上相当于C++中的函数指针. C++用函数指针获取函数的入口地址, 然后通过这个指针 ...

  2. 关于TabLayout+ViewPager组合实现多页面滑动

    转载请注明出处:http://blog.csdn.net/ht_android/article/details/46647711 在android提供的design library中新增了一个控件,叫 ...

  3. python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法

    python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法 在Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数 ...

  4. Oracle数据迁移笔记-Rownum与序列的自增长的组合用法技巧

    Rownum与序列的自增长的组合用法技巧 根据序列自增长的步长规律,结合表行记录Rownum值的规则批量生成表的行记录主键的用法技巧 案例如下: CREATE OR REPLACE PROCEDURE ...

  5. TabLayout + ViewPager

    一.实现思路 1.在build.gradle中添加依赖,例如: compile 'com.android.support:support-v4:23.4.0'compile 'com.android. ...

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

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

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

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

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

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

  9. 安卓TabLayout+ViewPager实现切页

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

随机推荐

  1. mysql 对表格加索引但原表格有重复数据

    1.把表中唯一数据搜索创建临时表,最后代替原先表. create table mmmmmm as SELECT * FROM meriadianannotation GROUP BY SeriesID ...

  2. Scrum Meeting Beta - 3

    Scrum Meeting Beta - 3 NewTeam 2017/12/1 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 完成布局方面的界面优化Issue #125 李奕 ...

  3. PAT 甲级 1144 The Missing Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343463260160 Given N integers, you ...

  4. php 多维数组排序

    PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以 ...

  5. Bootstrap-tagsinput标系统使用心得

    最近工作中由于需求使用到了Bootstrap-tagsinput标系统,我的需求是: 1)能够从后台数据库获取标签信息展示到前端页面: 2)能够实现输入标签添加到后台,并ajax刷新页面: 3)能够实 ...

  6. 我们为什么要使用Spring Cloud?

    我们为什么要使用Spring Cloud? 两个需要好好看看: Spring Boot Spring Clude Spring Cloud是一个集成了众多开源的框架,利用Spring Boot的开发便 ...

  7. BZOJ 2131 圈地计划(最小割+黑白染色)

    类似于happiness的一道题,容易想到最小割的做法. 但是不同的是那一道题是相邻的如果相同则有收益,这题是相邻的不同才有收益. 转化到建图上面时,会发现,两个相邻的点连的边容量会是负数.. 有一种 ...

  8. 【JavaScript】checkBox的多选行<tr>信息获取

    页面的列表table显示(后台model.addAttribute("page", page);传来page信息,page通过foreach标签迭代展示表格数据): <!-- ...

  9. BZOJ4923 K小值查询(splay)

    容易想到建一棵平衡树,修改时打上标记即可.但是修改会导致平衡树结构被破坏.注意到实际上只有[k+1,2k)这一部分数在平衡树中的位置会被改变,所以对这一部分暴力修改,因为每次都会使其至少减小一半,复杂 ...

  10. 【比赛】HNOI2018 排列

    这题原题... 这题题面七绕八绕,有点麻烦,反正最后转化就是一棵树,每个点有一个值,要把所有点选完,要求选择一个点必须是它的父亲和祖先已经全部被选了,贡献是这个点的权值乘上它被选择的排名 如果一个点是 ...