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);

    //如果之前沒有創(chuàng)建,創(chuàng)建新的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;
                //
        }
        //把創(chuàng)建的Fragment  存起來
        mFragments.put(index,fragment);
    }
    return fragment;
   }
}

樣式的編寫

 <style name="CustomTabLayoutTextAppearance"
    parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse">
    <item name="android:textSize">15sp</item>
 </style>
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,211評論 25 708
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽默認的外補...
    _Yfling閱讀 14,168評論 1 92
  • 煎熬和花露水的芬芳散盡,思念和蟬鳴一起飄遠,再見啦,夏天。晴空一鶴排云上,便引詩情到碧霄,秋天,我來了!今天終于感...
    何時再出發(fā)閱讀 165評論 1 0
  • 作者/賈仕杰 在山坡上讀一首詩 是為了等一片落葉飄零 看流水的影子 卻忘記了樹的年輪 我不念你的時候 心靈便長滿了...
    杰出道閱讀 237評論 0 0
  • 玫瑰好看吧 可是有刺 陽光溫暖吧 可是擁抱不了 我在等 可是你在哪呢
    就是簡單點閱讀 156評論 0 0

友情鏈接更多精彩內容