笔记(二)TabLayout + ViewPager + FragmentPagerAdapter 组合用法
- 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 组合用法的更多相关文章
- [读书笔记]C#学习笔记二: 委托和事件的用法及不同.
前言: C#委托是什么 c#中的委托可以理解为函数的一个包装, 它使得C#中的函数可以作为参数来被传递, 这在作用上相当于C++中的函数指针. C++用函数指针获取函数的入口地址, 然后通过这个指针 ...
- 关于TabLayout+ViewPager组合实现多页面滑动
转载请注明出处:http://blog.csdn.net/ht_android/article/details/46647711 在android提供的design library中新增了一个控件,叫 ...
- python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法
python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法 在Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数 ...
- Oracle数据迁移笔记-Rownum与序列的自增长的组合用法技巧
Rownum与序列的自增长的组合用法技巧 根据序列自增长的步长规律,结合表行记录Rownum值的规则批量生成表的行记录主键的用法技巧 案例如下: CREATE OR REPLACE PROCEDURE ...
- TabLayout + ViewPager
一.实现思路 1.在build.gradle中添加依赖,例如: compile 'com.android.support:support-v4:23.4.0'compile 'com.android. ...
- 介绍三个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 一些重要 ...
随机推荐
- 【最小生成树+LCA】Imperial roads
http://codeforces.com/gym/101889 I 先跑一遍最小生成树,把经过的边和答案记录下来 对于每个询问的边,显然如果处于MST中,答案不变 如果不在MST中,假设这条边连上了 ...
- CodeForces 479C Exams 贪心
题目: C. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- web表格代码(5)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- PHP中继承
继承 基本概念 将一个类A中的特性信息,传递到另一个类B中,此时就称为: B继承A A派生出B: 基本语法: 几个基本概念 继承:一个类从另一个已有的类获得其特性,称为继承. 派生:从一个已有的类产生 ...
- SQL Inserted和deleted详解
create trigger updateDeleteTime on user for update as begin update user set UpdateTime=(getdate()) f ...
- 第96天:CSS3 背景详解
一.背景大小 background: url("images/bg.jpg") no-repeat;控制背景的大小1.具体数值background-size: 500px 500p ...
- scoket常用函数简单介绍
scoket: 是一种抽象层,应用程序通过它来发送和接收数据,使用socket可以将应用程序添加到网络中,与处于同一网络中的其他应用程序进行通信. 简单来说,scoket提供了程序内部与外界通道的 ...
- shell的uniq命令
uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用. uniq 可检查文本文件中重复出现的行列. 命令语法: uniq [-c/d/D/u/i] [-f Fields ...
- Win10 安装 Linux 子系统
Win10 安装 Linux 子系统 因为最近要使用Linux搭服务器,但是用远程的话延迟很烦,用双系统切换很麻烦,用虚拟机又会有点卡,刚好Windows10最近更新了正式版的WSL(windows下 ...
- 基于注解的spring mvc 中使用 ajax json 的model
在 Spring mvc3中,响应.接受 JSON都十分方便. 使用注解@ResponseBody可以将结果(一个包含字符串和JavaBean的Map),转换成JSON. 使用 @RequestBod ...