笔记(二)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 一些重要 ...
随机推荐
- flask验证登录学习过程(1)---准备
对应flask的接口开发,目前自己可以熟练的进行.但是深入到更基础的,从注册到验证登录的过程一直不是特别清楚. 趁着年度不是特别忙的时候,特意去学习加强一下.把这个过程记录在此处. 首先是规划一个项目 ...
- 将 Spring 和 Hibernate 与 WebSphere Application Server 一起使用
本文摘要 如果您考虑将 Spring 或 Hibernate 与 IBM® WebSphere® Application Server 一起使用,则本文向您阐述了如何配置这些框架,以适用于 WebSp ...
- package分析
由于大家对package的使用存在太多困惑,我在这里将自己对于package的使用的领悟进行一点总结: package中所存放的文件 所有文件,不过一般分一下就分这三种 1,java程序源文件,扩展名 ...
- eclipse取消validation验证
点击按钮如下:window-Preferences-Validation.如图. 然后把build里面的都取消.即可.
- 1014 我的C语言文法定义与C程序推导过程
程序> -> <外部声明> | <程序> <外部声明> <外部声明> -> <函数定义> | <声明> < ...
- ctf实验平台-成绩单
题目链接:http://120.24.86.145:8002/chengjidan/ 平台地址:http://123.206.31.85/ 第一步:暴库 id=-1' union select 1,2 ...
- MAVEN pom.xml 解读
POM全称是Project Object Model,即项目对象模型.pom.xml是maven的项目描述文件,它类似与antx的project.xml文件.pom.xml文件以xml的 形式描述项 ...
- docker-py execute echo无效
错误写法: cli.execute('9b2606a50304','echo "bibo">/tmp/1.txt') 争取写法: cli.execute('9b2606a ...
- 关于ADO一个容易被忽视的问题!UpdateBatch [问题点数:0分]
这是一个常见但容易被忽视的问题,旧贴有问及但没答案,因此提高分数.相信大家常这样使用Cache模式:ADOConnection1.BeginTrans;Try ADODataSet1.UpdateBa ...
- 个人博客开发-01-nodeJs项目搭建
// window系统下 1.nodeJs 安装 nodeJs 安装 看 这里 , 先下载再下一步下一步就OK了,我的是在C盘里安装的. 安装完以后 按 win + R ,在弹出的小框框里输入 CM ...