ViewPager+Fragment+TabLayout案例

第一种方法:

1.xml设置TabLayout与ViewPager

<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmls:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"    
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
    tools:context="com.edu.cumulus.tablayoutdemo.TabViewPagerActivity">

<android:support:v4.view.ViewPager
    android:id="@+id/vp"
     android:layout_width="match_parent"
    android:layout_height="wrap_content">
 </android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true">
 </android.support.design.widget.TabLayout>
</RelativeLayout/>

2.为ViewPager创建适配器

    public class MianAdapter extends FragmentPagerAdapter{
    private ArrayList<Fragment> fragments;
   public MainAdapter(FragmentManager fm,     ArrayList<Fragment> fragments) {
          super(fm);
          this.fragments = fragments;
}
    public MainAdapter(FragmentManager fm) {
          super(fm);
}

@Override
    public Fragment getItem(int i) {
          return fragments.get(i);
}

@Override
    public int getCount() {
        return fragments.size();
}
}

3.代码设置TabLayout和ViewPager绑定
//找控件
//数据源
//创建适配器

 MainAdapter mainAdapter=new  MainAdapter(getSupportFragmentManager(),fragments)
//设置适配器
vp.setAdapter(mainAdapter);
  //联动
  tab.setupWithViewPager(vp);
  tab.getTabAt(0).setText("首页");
  tab.getTabAt(1).setText("资讯");
  tab.getTabAt(2).setText("直播");
  tab.getTabAt(3).setText("我");

#######第二种方法
首先式是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#ffffff"
        android:orientation="vertical">
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_gank"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            app:tabBackground="@drawable/ripple_tab_bg"
            app:tabIndicatorColor="#ffce3d3a"
            app:tabSelectedTextColor="#ffce3d3a"
            app:tabTextColor="#585858" />

    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_gank"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff2f4f5">

    </android.support.v4.view.ViewPager>

</LinearLayout>
</lLinearLayout>

重写FragmentAdapter

 public class  MainFragmentAdapter extends FragmentPagerAdapter{L

List<Fragment> list;
List<String> title;

public MyFragmentAdapter(FragmentManager fm) {
    super(fm);
    }

public MyFragmentAdapter(FragmentManager fm, List<Fragment> list, List<String> title) {
    super(fm);
    this.list = list;
    this.title = title;
}

@Override
public Fragment getItem(int position) {
    return list.get(position);
}

@Override
public int getCount() {
    return list.size();
}

/*让TabLayout能获取到title*/
@Override
public CharSequence getPageTitle(int position) {
    return title.get(position);
}

}
主要是重写getPageTitle()方法
代码设置TabLayout和ViewPager绑定

 // tablayout
TabLayout tabLayout = (TabLayout)                   findViewById(R.id.tab_layout);
// vp
mViewPager = (ViewPager)     findViewById(R.id.container);

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  mViewPager.setAdapter(mSectionsPagerAdapter);
// 绑定,要在viewpager设置完数据后,调用此方法,否则不显示 tabs文本
        tabLayout.setupWithViewPager(mViewPager);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、主页面布局: 添加Tablayout和ViewPager两个控件 PS:使用前要先添加支持库: ViewPag...
    V_boomboom阅读 7,703评论 0 6
  • 通常在ViewPager的上方,我们都会放一个标签指示器与ViewPager进行联动。以前,我们大多使用的是Git...
    DoAndKeep阅读 79,714评论 36 110
  • 前言: 本章节要对 ViewPager 以及 PagerAdapter 有所了解,该章节部分代码沿用我的这个帖子 ...
    RunHuaOil阅读 11,096评论 1 14
  • 一、简述 TabLayout是Android Support Design库的新控件,可以用来实现开源框架View...
    CQ_TYL阅读 5,653评论 0 5
  • 莫兮学校今天开运动会了。 莫兮根本无暇顾及比赛,因为公务繁忙。而且比赛也无聊,只有看到比较刺激的场面,莫兮才会稍微...
    已疯的天才阅读 929评论 0 1