Android Design Supportコントロール紹介のTabLayout

10667 ワード

     [Android Design Support Library      ( )](http://blog.csdn.net/true100/article/details/50593636)        TabLayout   。
                    ,     TabPageIndicator+ViewPager   。      TabLayout+ViewPager        ,              。         !
メインインターフェースレイアウト
   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical">
    <!--tabTextColor      -->
    <!--tabSelectedTextColor        -->
    <!--tabIndicatorColor      -->
    <!--tabIndicatorHeight      -->
    <android.support.design.widget.TabLayout  android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabIndicatorColor="@android:color/holo_green_dark" app:tabIndicatorHeight="4dp" app:tabSelectedTextColor="@android:color/holo_red_dark" app:tabTextColor="@android:color/black"></android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager  android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout> 
使用するFragmentページの簡単なレイアウト
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp">

    <TextView  android:id="@+id/content_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" />
</RelativeLayout>
Fraament類
public class TestFragment extends Fragment {
    private String content;
    private TextView content_tv;
    private View fragmentView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        content=(String)getArguments().get("content");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        fragmentView= inflater.inflate(R.layout.content_main,null);
        content_tv=(TextView)fragmentView.findViewById(R.id.content_tv);
        content_tv.setText(content);
        return fragmentView;
    }
}
View Pagerアダプター類
/** * Created by: ${ldm} * time : 2016/1/27 15:34 */
public class TestAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;
    private List<String>titles;
    public TestAdapter(FragmentManager fm,List<String>titles,List<Fragment> fragments) {
        super(fm);
        this.fragments=fragments;
        this.titles=titles;
    }

    /** * Return the Fragment associated with a specified position. * * @param position */
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    /** * Return the number of views available. */
    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}
ホームページコード類
public class MainActivity extends FragmentActivity {
    //        
    private TabLayout tab_layout;
    private ViewPager viewpager;
    private TestAdapter mAdapter;
    private List<Fragment> fragments;
    private List<String> titles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initViewPages();
    }

    private void initViewPages() {
        fragments = new ArrayList<>();
        titles = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            String title="TAB" + i;
            titles.add(title);
            tab_layout.addTab(tab_layout.newTab().setText(title));//  Tab  
            Fragment fragment = new TestFragment();
            Bundle bundle = new Bundle();
            bundle.putString("content", "   " + i + " Fragment  ");
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }

        mAdapter = new TestAdapter(this.getSupportFragmentManager(), titles, fragments);
        viewpager.setAdapter(mAdapter);
        tab_layout.setupWithViewPager(viewpager);
        tab_layout.setTabsFromPagerAdapter(mAdapter);
    }

    private void initViews() {
        tab_layout = (TabLayout) findViewById(R.id.tab_layout);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
    }

}
効果は図の通りです