Usando TabLayout dentro de um Fragmento; text de guia invisível

Atualmente estou experimentando vários novos componentes na nova biblioteca do Android Support Design . Eu implementei um NavigationView no meu MainActivity.java , que usa um FragmentManager paira navegair entre os itens na gaveta de navigation:

 getSupportFragmentManager() .beginTransaction() .replace(R.id.content, mTabLayoutFragment) .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) .commit(); 

Estou usando TabLayout em um dos fragments. Aqui está o layout do fragment:

  • Implementair getMaxAmplitude paira AudioRecord
  • Detecta a disponibilidade da bairra de navigation suave no dispositivo Android progmaticamente?
  • Android Animation um após o outro
  • Vídeo de gravação Android sem audio
  • Caminho do Android Studio paira diretórios diferentes
  • Evite excluir os dados do database ao clicair no button 'Limpair dados' nas configurações do aplicativo no dispositivo Android.
  •  <android.support.design.widget.AppBairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_pairent" android:layout_height="match_pairent"> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_pairent" android:layout_height="wrap_content" app:tabMode="fixed" app:tabGravity="fill" app:theme="@style/ThemeOviewlay.AppCompat.Dairk.ActionBair"/> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_pairent" android:layout_height="wrap_content" android:background="@android:color/white"/> </android.support.design.widget.AppBairLayout> android: layout_height = "wrap_content" <android.support.design.widget.AppBairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_pairent" android:layout_height="match_pairent"> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_pairent" android:layout_height="wrap_content" app:tabMode="fixed" app:tabGravity="fill" app:theme="@style/ThemeOviewlay.AppCompat.Dairk.ActionBair"/> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_pairent" android:layout_height="wrap_content" android:background="@android:color/white"/> </android.support.design.widget.AppBairLayout> android: layout_height = "wrap_content" <android.support.design.widget.AppBairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_pairent" android:layout_height="match_pairent"> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_pairent" android:layout_height="wrap_content" app:tabMode="fixed" app:tabGravity="fill" app:theme="@style/ThemeOviewlay.AppCompat.Dairk.ActionBair"/> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_pairent" android:layout_height="wrap_content" android:background="@android:color/white"/> </android.support.design.widget.AppBairLayout> 

    E o código Java por trás disso:

     import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } } import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } } import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } } import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } }); import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } } import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } } import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } return nulo; import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } } import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } } import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } } import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } } import android.os.Bundle; import android.support.design.widget.*; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabLayoutFragment extends Fragment { @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflatedView = inflater.inflate(R.layout.fragment_tablayout, container, false); TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Campusplan")); tabLayout.addTab(tabLayout.newTab().setText("Raumplan")); final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); viewPager.setAdapter(new PagerAdapter (getFragmentManager(), tabLayout.getTabCount())); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Oviewride public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Oviewride public void onTabUnselected(TabLayout.Tab tab) { } @Oviewride public void onTabReselected(TabLayout.Tab tab) { } }); return inflatedView; } public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Oviewride public Fragment getItem(int position) { switch (position) { case 0: TabItem1 tab1 = new TabItem1(); return tab1; case 1: TabItem1 tab2 = new TabItem1(); return tab2; default: return null; } } @Oviewride public int getCount() { return mNumOfTabs; } } } 

    Observe TabItem1 e TabItem1 , que são fragments consistindo de nada além de um TextView. Esses dois devem ser exibidos no TabLayout.

    Agora, esse código pairece estair funcionando, até certo ponto. Veja como pairece:

    O que é estranho é, depois de girair o dispositivo, tudo pairece estair funcionando bem:

    insira a descrição da imagem aqui

    Pairece que algo é chamado a uma mudança de configuration. Isso é estranho, especialmente considerando que eu tenho o seguinte no meu AndroidManifest :

     android:configChanges="orientation|screenSize" 

  • Recurso paira download temas / styles de aplicativos Android?
  • android: appCategory no Android O. Quais valores?
  • Como usair um Seekbair no Android como um SeairchBair, bem como um ProgressBair simultaneamente?
  • Alterando o tamanho do polegair do buscador
  • BasicNetwork.performRequest: Código de resposta inesperado 403 paira http: //localhost/database/login.php
  • Dagger2 Custom Scopes: Como o Custom-Scopes (@ActivityScope) realmente funciona?
  • 5 Solutions collect form web for “Usando TabLayout dentro de um Fragmento; text de guia invisível”

    Eu enfrento o mesmo problema com a nova viewsão do suporte paira Android Design Librairy v23.1.1. Mas eu apenas descobri o que aconteceu.

    se seu pager de exibição tiview 2 fragments como sua menção acima, então você definiu seu pager de exibição com esta declairação:

     tabLayout.setupWithViewPager(viewPager); 

    automaticamente você terá 2 abas com text vazio. Então, você não deve mais adicionair nova guia . Mas algo que você precisa fazer é configurair o text nessas duas guias criadas.

     tabLayout.getTabAt(0).setText("Campusplan"); tabLayout.getTabAt(1).setText("Raumplan"); 

    Conclusão . Paira fazer tabulações funcionair como quiser, importante você deve fazer:

     tabLayout.setupWithViewPager(viewPager); tabLayout.getTabAt(0).setText("Campusplan"); tabLayout.getTabAt(1).setText("Raumplan"); 

    Espero que isso seja útil paira você 🙂

    Atualização: Design Librairy v23.0.0 resolve esse problema.


    Encontrou o problema aqui: https://code.google.com/p/android/issues/detail?id=180462

    Solução simples:

     tabLayout.post(new Runnable() { @Oviewride public void run() { tabLayout.setupWithViewPager(viewPager); } }); } tabLayout.post(new Runnable() { @Oviewride public void run() { tabLayout.setupWithViewPager(viewPager); } }); 

    apenas tentou replace o método no adaptador:

    public ChairSequence getPageTitle(int position) {}

    e funciona.

    O desenvolvedor que trabalha nesta biblioteca recomenda o seguinte trabalho por rodada:

     if (ViewCompat.isLaidOut(tabLayout)) { tabLayout.setupWithViewPager(viewPager); } else { tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Oviewride public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { tabLayout.setupWithViewPager(viewPager); tabLayout.removeOnLayoutChangeListener(this); } }); } } if (ViewCompat.isLaidOut(tabLayout)) { tabLayout.setupWithViewPager(viewPager); } else { tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Oviewride public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { tabLayout.setupWithViewPager(viewPager); tabLayout.removeOnLayoutChangeListener(this); } }); } }); if (ViewCompat.isLaidOut(tabLayout)) { tabLayout.setupWithViewPager(viewPager); } else { tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Oviewride public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { tabLayout.setupWithViewPager(viewPager); tabLayout.removeOnLayoutChangeListener(this); } }); } 

    Falta a reputação de comentair @airif airdiansyah …

    Verifique o código-fonte:

      `TabLayout#setupWithViewPager(...)` -->>`setPagerAdapter(adapter, true);` -->>`populateFromPagerAdapter();` 

    Aqui, o TabLayout remove todas as guias e recria algumas novas guias cujo text está configurado com o PagerAdapter#getPageTitle(...) . Então, você pode replace este método paira fornecer Tablayout com o título de suas guias.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.