笔记(二)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 一些重要 ...
随机推荐
- MacOS下安装BeautifulSoup库及使用
BeautifulSoup简介 BeautifulSoup库是一个强大的python第三方库,它可以解析html进行解析,并提取信息. 安装BeautifulSoup 打开终端,输入命令: pip3 ...
- tomcat配置服务
1.在server中右键添加tomcat 2.双击tomcat打开配置窗口添加jvm参数 -Doapath="C:\exeye-workspace\exEyeWeb\oadoc" ...
- JavaScript设计模式学习之路——面向对象的思想
今天,我拿到了张容铭写的这本<JavaScript设计模式>这本书,开始了关于JavaScript更深一点的学习. 看到这本书开始的时候,虽然之前通过看书.一些比较好的视频的讲解,对Jav ...
- Tomcat指定JDK路径
一.应用实例 一般情况下一台服务器只跑一个业务,那么就直接配置一套环境,设置好Java环境变量即可.某些时候一台服务器上会安装多个业务,而且各个业务需要的JDK版本各不相同,或者为了使业务独立开来,需 ...
- 第93天:CSS3 中边框详解
CSS3 边框详解 其中边框圆角.边框阴影属性,应用十分广泛,兼容性也相对较好,具有符合渐进增强原则的特征,我们需要重点掌握. 一.边框圆角 border-radius 每个角可以设置两个值 ...
- Underscore.js工具库
Underscore 是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象. 他解决了这个问题:“如果我面对一个空白的 HTML ...
- UVALive - 4975_Casting Spells
题意很简单,给你一个字符串,要求你求出一个最长的形似于w(wr)w(wr)的最长连续子串的长度.wr表示w的逆序串. 在这里大家很容易就能想到Manacher算法求回文串.没有错,就是这个. 算法的详 ...
- 【uoj#22】[UR #1]外星人 组合数学+dp
题目描述 给你一个长度为 $n$ 的序列 $\{a_i\}$ 和一个数 $x$ ,对于任意一个 $1\sim n$ 的排列 $\{p_i\}$ ,从 $1$ 到 $n$ 依次执行 $x=x\ \tex ...
- 【bzoj3774】最优选择 网络流最小割
题目描述 小N手上有一个N*M的方格图,控制某一个点要付出Aij的代价,然后某个点如果被控制了,或者他周围的所有点(上下左右)都被控制了,那么他就算是被选择了的.一个点如果被选择了,那么可以得到Bij ...
- Eclipse 保存代码时,不自动换行设置
Eclipse在保存代码时,总是自动换行.尤其是注释,换行后的注释读起来就很混乱.后来发现是在保存文件时设置了自动格式化代码的原因. 关闭自动格式代码设置: windows-->Preferen ...