Back to project page MaterialTabHost.
The source code is released under:
Apache License
If you think the Android project MaterialTabHost listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* Copyright (C) 2014 Yuki Anzai anzai.y.aa@gmail.com */*ww w. j a v a 2 s . c om*/ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.yanzm.mth.sample; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBarActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import net.yanzm.mth.MaterialTabHost; import java.util.Locale; public class SampleActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sample); if (getSupportActionBar() != null) { getSupportActionBar().setElevation(0); } MaterialTabHost tabHost = (MaterialTabHost) findViewById(android.R.id.tabhost); tabHost.setType(MaterialTabHost.Type.FullScreenWidth); // tabHost.setType(MaterialTabHost.Type.Centered); // tabHost.setType(MaterialTabHost.Type.LeftOffset); SectionsPagerAdapter pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); for (int i = 0; i < pagerAdapter.getCount(); i++) { tabHost.addTab(pagerAdapter.getPageTitle(i)); } final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); viewPager.setAdapter(pagerAdapter); viewPager.setOnPageChangeListener(tabHost); tabHost.setOnTabChangeListener(new MaterialTabHost.OnTabChangeListener() { @Override public void onTabSelected(int position) { viewPager.setCurrentItem(position); } }); } public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). return PlaceholderFragment.newInstance(position + 1); } @Override public int getCount() { // Show 3 total pages. return 3; } @Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.title_section1).toUpperCase(l); case 1: return getString(R.string.title_section2).toUpperCase(l); case 2: return getString(R.string.title_section3).toUpperCase(l); } return null; } } public static class PlaceholderFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; /** * Returns a new instance of this fragment for the given section * number. */ public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_sample, container, false); TextView tv = (TextView) rootView.findViewById(R.id.section_label); tv.setText("Here is page " + getArguments().getInt(ARG_SECTION_NUMBER)); return rootView; } } }