TabLayout+ViewPager结合使用

1.布局的写法

<LinearLayout
    android:id="@+id/tab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black_alpha_5"
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/status_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        app:tabIndicatorColor="@color/tab_selected_line_def"
        app:tabSelectedTextColor="@color/tab_text_selected_def"
        app:tabTextColor="@color/tab_text_normal_def"
     app:tabTextAppearance="@style/CustomTabLayoutTextAppearance"
        />


</LinearLayout>


<android.support.v4.view.ViewPager
    android:id="@+id/main_viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

2.Activity的写法

 @BindView(R.id.status_bar)
     LinearLayout statusBar;
 @BindView(R.id.tab_layout)
    TabLayout tabLayout;
 @BindView(R.id.main_viewpager)
    ViewPager mainViewPager;
//viewPager的适配器
 private FixPagerAdapter fixPagerAdapter;
private String[] titles={"推荐","分类","排行","管理","我的"};
private List<Fragment> fragments;

//对fragment进行绑定
private void intViewPagerFragment() {

    fixPagerAdapter=new FixPagerAdapter(getSupportFragmentManager());

    fragments=new ArrayList<>();
    for(int i=0;i<titles.length;i++){
    //    new RecommendFragment();
        //
        fragments.add(FragmentFactory.createFragment(i));
    }
    fixPagerAdapter.setTitles(titles);
    fixPagerAdapter.setFragments(fragments);

    mainViewPager.setAdapter(fixPagerAdapter);
    //将ViewPager与TabLayout绑定
    tabLayout.setupWithViewPager(mainViewPager);
    //显示样式
    tabLayout.setTabMode(TabLayout.MODE_FIXED);

}

fragment工程类的编写

 public class FragmentFactory {

/**
 * 推荐
 */
public static final int TAB_RECOMMEND=0;

/**
 * 分类
 */
public static final int TAB_CATEGORY=1;

/**
 * 排行
 */
public static final int TAB_TOP=2;

/**
 * 管理
 */
public static final int TAB_APPMANAGER=3;

/**
 * 我的
 */
public static final int TAB_MY=4;

private static Map<Integer,BaseFragment> mFragments=
        new HashMap<Integer,BaseFragment>();

public static BaseFragment createFragment(int index){
    BaseFragment fragment= mFragments.get(index);

    //如果之前没有创建,创建新的Fragment
    if(fragment==null){
        switch (index){
            case TAB_RECOMMEND:
                fragment=new RecommendFragment();
                break;
            case TAB_CATEGORY:
                fragment=new CategoryFragment();
                break;
            case TAB_TOP:
                fragment=new TopFragment();
                break;
            case TAB_APPMANAGER:
                fragment=new AppManagerFragment();
                break;
            case TAB_MY:
                fragment=new MyFragment();
                break;
                //
        }
        //把创建的Fragment  存起来
        mFragments.put(index,fragment);
    }
    return fragment;
   }
}

样式的编写

 <style name="CustomTabLayoutTextAppearance"
    parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
    <item name="android:textSize">15sp</item>
 </style>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,236评论 25 708
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,174评论 1 92
  • 煎熬和花露水的芬芳散尽,思念和蝉鸣一起飘远,再见啦,夏天。晴空一鹤排云上,便引诗情到碧霄,秋天,我来了!今天终于感...
    何时再出发阅读 165评论 1 0
  • 作者/贾仕杰 在山坡上读一首诗 是为了等一片落叶飘零 看流水的影子 却忘记了树的年轮 我不念你的时候 心灵便长满了...
    杰出道阅读 237评论 0 0
  • 玫瑰好看吧 可是有刺 阳光温暖吧 可是拥抱不了 我在等 可是你在哪呢
    就是简单点阅读 156评论 0 0

友情链接更多精彩内容