笔记(二)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);
时间: 2024-08-29 05:41:25

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

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

前言:  C#委托是什么 c#中的委托可以理解为函数的一个包装, 它使得C#中的函数可以作为参数来被传递, 这在作用上相当于C++中的函数指针. C++用函数指针获取函数的入口地址, 然后通过这个指针来实现对函数的操作. 委托的定义和方法的定义类似, 只是在定义的前面多了一个delegate关键字. 正文: 委托可以被视为一个更高级的指针,它不仅仅能把地址传指向另一个函数,而且还能传递参数,返回值等多个信息. 系统还为委托对象自动生成了同步,异步的调用方式,开发人员使用BeginInvoke,E

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

转载请注明出处:http://blog.csdn.net/ht_android/article/details/46647711 在android提供的design library中新增了一个控件,叫TabLayout,它继承自HorizontalScrollView,可以实现android中多页面滑动切换效果.但是一般需要和ViewPager组合使用,官方API地址:https://developer.android.com/reference/android/support/design/w

Android学习笔记二十九之SwipeRefreshLayout、RecyclerView和CardView

Android学习笔记二十九之SwipeRefreshLayout.RecyclerView和CardView 前面我们介绍了AlertDialog和几个常用的Dialog,ProgressDialog进度条提示框.DatePickerDialog日期选择对话框和TimePickerDialog时间选择对话框.这一节我们介绍几个新的API控件SwipeRefreshLayout.RecyclerView和CardView,这几个API控件都是google在Android5.0推出的.下面我们来学

Android学习笔记二十之Toast吐司、Notification通知、PopupWindow弹出窗

Android学习笔记二十之Toast吐司.Notification通知.PopupWindow弹出窗 Toast吐司 Toast吐司是我们经常用到的一个控件,Toast是AndroidOS用来显示消息的一种机制,它与Dialog不同,Toast不会获取到焦点,通常显示一段时间之后就会自动消失,下面我们来介绍Toast的几种常用方式: 第一种,默认显示方式,也是最常用的方式: Toast.makeText(MainActivity.this, "这是默认的显示方式", Toast.LE

TabLayout+ViewPager 标题不显示问题

第一次用TabLayout+ViewPager 组合在布局中写好了三个标题预览没问题而且也设置了 app:tabIndicatorColor="@color/colorAccent" app:tabSelectedTextColor="@color/colorAccent"app:tabTextColor="@color/button_nav_font_default"三个属性都设置,当运行在手机上的时候显示空白刚开始以为是手机问题(华为)换了小

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

该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解Android 卷Ⅰ,Ⅱ,Ⅲ>中的相关知识,另外也借鉴了其他的优质博客,在此向各位大神表示感谢,膜拜!!! 前言 上一篇文章中我们使用底部导航+Fragment的方式实现了Android主流App中大都存在的设计.并命名其为"Fragment最佳实践",作为想到单独使用Fragment的用

《卓有成效的程序员》----读书笔记二

六大方面对比Launchy和TypeAndRun(TAR) 对于快速启动工具,很多人都有自己的偏好,多次听到朋友介绍Launchy的好,虽然自己一直在使用着TAR,还是克制不住对于好软件的渴求,下载Launchy进行试用.很多软件都是有一个试用期的,也许新的软件确实不错,但是你习惯了以前使用的那个软件.今天就比较客观的将Launchy和TAR进行一下对比,从界面.上手速度到功能.自定义,以及软件的稳定性.占用资源进行详细的比较. [界面美观]Launchy:毫无疑问这是它的强项.1.0正式版自带

Android学习笔记二十.使用ContentProvider实现数据共享(二).URI...工具类

一.UriMatcher与ContentUris工具类 UriMatcher 1.功能概述 开发ContentProvider时所实现的query().insert().delete().update()方法的第一个参数为Uri参数,该参数由ContentResolver调用这些方法时传入.在上一篇博文中的实例,并没有真正对数据进行操作,因此ContentProvider并未对Uri参数进行任何判断.所以为了确定该ContentProvider实际能处理的Uri,以确定每个方法中Uri参数所操作

angular学习笔记(二十八)-$http(6)-使用ngResource模块构建RESTful架构

ngResource模块是angular专门为RESTful架构而设计的一个模块,它提供了'$resource'模块,$resource模块是基于$http的一个封装.下面来看看它的详细用法 1.引入angular-resource.min.js文件 2.在模块中依赖ngResourece,在服务中注入$resource var HttpREST = angular.module('HttpREST',['ngResource']); HttpREST.factory('cardResource