TabLayout簡單例子

先上截圖

(TabLayout + ViewPager(Fragment))

這裏寫圖片描述

步驟

1.首先是佈局文件:

<android.support.design.widget.AppBarLayout
    android :layout_width="match_parent"
    android :layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android :id="@+id/toolbar"
        android :layout_width="match_parent"
        android :layout_height="?attr/actionBarSize"
        android :background="?attr/colorPrimary"
        app :layout_scrollFlags="scroll|enterAlways"
        app :theme="@style/ThemeOverlay.AppCompat.Dark" />

    <android.support.design.widget.TabLayout xmlns:tabl= "http://schemas.android.com/apk/res-auto"
        android :id="@+id/tab"
        android :layout_width="match_parent"
        android :layout_height="wrap_content"
        android :background="@color/colorPrimary"
        tabl :tabSelectedTextColor="@color/colorPrimaryTextLight"
        tabl :tabTextColor="@color/colorSecondaryTextLight" />

</android.support.design.widget.AppBarLayout>


<android.support.v4.view.ViewPager
    android :id="@+id/viewpager"
    android :layout_width="match_parent"
    android :layout_height="match_parent"
    android :background="@android:color/white "
    app :layout_behavior="@string/appbar_scrolling_view_behavior" />

2.在代碼中添加TabLayout與ViewPager,並關聯一起即可

//界面數據 
String[] tabString = { "Tab1" ,"Tab2" , "Tab3"} ;
ArrayList<Fragment> mFragments = new ArrayList<>(); 
for ( int i = 0 ; i < tabString. length ; i++) { 
    MyPageFragment mFragment = new MyPageFragment() ;
    mFragments.add(mFragment) ;
}

// 初始化ViewPager的適配器 
MyPageAdapter mViewPagerAdapter = new MyPageAdapter(getSupportFragmentManager() , tabString, mFragments) ;
viewPager .setAdapter(mViewPagerAdapter); 
//將Tab與ViewPager關聯(動畫效果同步) 
tabLayout .setupWithViewPager(viewPager ) ;

接下來即缺什麼補什麼就行了:

3. 一個簡單的Fragment:

public class MyPageFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.page, container, false);
    }
}

4.R.layout.page內容:

<? xml version="1.0" encoding= "utf-8"?>
<TextView xmlns: android="http://schemas.android.com/apk/res/android"
    android :layout_width="match_parent"
    android :layout_height="match_parent"
    android :gravity="center"
    android :text="text"
    android :textColor="@android:color/black"
    android :orientation="vertical"/> 

5. 最後就剩ViewPager的適配器:

public class MyPageAdapter extends FragmentStatePagerAdapter {

    private String[] mTitles;
    private List<Fragment> mFragments;

    public MyPageAdapter(FragmentManager fm , String[] mTitles, List<Fragment> mFragments) {
        super (fm);
        this. mTitles = mTitles;
        this. mFragments = mFragments;
    }

    @Override
    public CharSequence getPageTitle( int position) {
        return mTitles[position];
    }

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

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

}
發佈了42 篇原創文章 · 獲贊 90 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章