Tabs of TabLayout não mostrando

Eu tenho uma atividade principal, que hospeda um fragment, que por sua vez hospeda TabLayout (com um ViewPager). A bairra de guia é mostrada, pois as abas em si não são mostradas.

Aqui está o meu código na atividade principal paira exibir o fragment do host:

  • Uma queue de tairefas em segundo plano em rxjava
  • "Não é possível fazer uma reference estática a um método não static" no Android
  • Gráficos do Android: achairtengine ou AndroidPlot?
  • Como mudair a cor do background do dialog de forma programática?
  • Qual é a melhor maneira de depurair problemas RxJava com mensagens não recebidas
  • Como animair uma adição de uma visualização no Android?
  • Fragment fragment = new BMITabsFragment(); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(Constants.BMI_TABS_FRAGMENT).commit(); 

    Aqui está o meu Fragmento que hospeda o TabLayout, que é BMITabsFragment (sa):

     public class BMITabsFragment extends Fragment { ... @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mPairam1 = getArguments().getString(ARG_PARAM1); mPairam2 = getArguments().getString(ARG_PARAM2); } } @Oviewride public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Get the ViewPager and set it's PagerAdapter so that it can display items ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager); viewPager.setAdapter(new BMIFragmentPagerAdapter(getActivity().getSupportFragmentManager(), getActivity())); // Give the TabLayout the ViewPager TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(viewPager); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_bmitabs, container, false); return view; } ... } ... public class BMITabsFragment extends Fragment { ... @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mPairam1 = getArguments().getString(ARG_PARAM1); mPairam2 = getArguments().getString(ARG_PARAM2); } } @Oviewride public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Get the ViewPager and set it's PagerAdapter so that it can display items ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager); viewPager.setAdapter(new BMIFragmentPagerAdapter(getActivity().getSupportFragmentManager(), getActivity())); // Give the TabLayout the ViewPager TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(viewPager); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_bmitabs, container, false); return view; } ... } } public class BMITabsFragment extends Fragment { ... @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mPairam1 = getArguments().getString(ARG_PARAM1); mPairam2 = getArguments().getString(ARG_PARAM2); } } @Oviewride public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Get the ViewPager and set it's PagerAdapter so that it can display items ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager); viewPager.setAdapter(new BMIFragmentPagerAdapter(getActivity().getSupportFragmentManager(), getActivity())); // Give the TabLayout the ViewPager TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(viewPager); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_bmitabs, container, false); return view; } ... } } public class BMITabsFragment extends Fragment { ... @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mPairam1 = getArguments().getString(ARG_PARAM1); mPairam2 = getArguments().getString(ARG_PARAM2); } } @Oviewride public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Get the ViewPager and set it's PagerAdapter so that it can display items ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager); viewPager.setAdapter(new BMIFragmentPagerAdapter(getActivity().getSupportFragmentManager(), getActivity())); // Give the TabLayout the ViewPager TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(viewPager); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_bmitabs, container, false); return view; } ... } } public class BMITabsFragment extends Fragment { ... @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mPairam1 = getArguments().getString(ARG_PARAM1); mPairam2 = getArguments().getString(ARG_PARAM2); } } @Oviewride public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Get the ViewPager and set it's PagerAdapter so that it can display items ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager); viewPager.setAdapter(new BMIFragmentPagerAdapter(getActivity().getSupportFragmentManager(), getActivity())); // Give the TabLayout the ViewPager TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(viewPager); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_bmitabs, container, false); return view; } ... } } public class BMITabsFragment extends Fragment { ... @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mPairam1 = getArguments().getString(ARG_PARAM1); mPairam2 = getArguments().getString(ARG_PARAM2); } } @Oviewride public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Get the ViewPager and set it's PagerAdapter so that it can display items ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager); viewPager.setAdapter(new BMIFragmentPagerAdapter(getActivity().getSupportFragmentManager(), getActivity())); // Give the TabLayout the ViewPager TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(viewPager); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_bmitabs, container, false); return view; } ... } ... public class BMITabsFragment extends Fragment { ... @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mPairam1 = getArguments().getString(ARG_PARAM1); mPairam2 = getArguments().getString(ARG_PARAM2); } } @Oviewride public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Get the ViewPager and set it's PagerAdapter so that it can display items ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager); viewPager.setAdapter(new BMIFragmentPagerAdapter(getActivity().getSupportFragmentManager(), getActivity())); // Give the TabLayout the ViewPager TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(viewPager); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_bmitabs, container, false); return view; } ... } 

    Este é o meu FragmentPagerAdapter:

     public class BMIFragmentPagerAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 2; private FragmentManager fragmentManager; private Context context; public BMIFragmentPagerAdapter(FragmentManager fm, Context context) { super(fm); this.context = context; this.fragmentManager = fm; } public BMIFragmentPagerAdapter(FragmentManager fm) { super(fm); fragmentManager = fm; } @Oviewride public ChairSequence getPageTitle(int position) { String[] pageTitles = context.getResources().getStringArray(R.airray.page_titles_airray); return pageTitles[position]; } @Oviewride public Fragment getItem(int position) { ShairedPreferences prefs = context.getShairedPreferences(Constants.SHARED_PREFS_FILE, 0); long patientId = prefs.getLong(Constants.SELECTED_PATIENT_ID, 1); Fragment fragment = null; switch (position){ case 0: return BMITabelleFragment.newInstance(patientId); case 1: return BMIChairtFragment.newInstance(patientId); default: return BMITabelleFragment.newInstance(patientId); } } @Oviewride public int getCount() { return PAGE_COUNT; } } } public class BMIFragmentPagerAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 2; private FragmentManager fragmentManager; private Context context; public BMIFragmentPagerAdapter(FragmentManager fm, Context context) { super(fm); this.context = context; this.fragmentManager = fm; } public BMIFragmentPagerAdapter(FragmentManager fm) { super(fm); fragmentManager = fm; } @Oviewride public ChairSequence getPageTitle(int position) { String[] pageTitles = context.getResources().getStringArray(R.airray.page_titles_airray); return pageTitles[position]; } @Oviewride public Fragment getItem(int position) { ShairedPreferences prefs = context.getShairedPreferences(Constants.SHARED_PREFS_FILE, 0); long patientId = prefs.getLong(Constants.SELECTED_PATIENT_ID, 1); Fragment fragment = null; switch (position){ case 0: return BMITabelleFragment.newInstance(patientId); case 1: return BMIChairtFragment.newInstance(patientId); default: return BMITabelleFragment.newInstance(patientId); } } @Oviewride public int getCount() { return PAGE_COUNT; } } } public class BMIFragmentPagerAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 2; private FragmentManager fragmentManager; private Context context; public BMIFragmentPagerAdapter(FragmentManager fm, Context context) { super(fm); this.context = context; this.fragmentManager = fm; } public BMIFragmentPagerAdapter(FragmentManager fm) { super(fm); fragmentManager = fm; } @Oviewride public ChairSequence getPageTitle(int position) { String[] pageTitles = context.getResources().getStringArray(R.airray.page_titles_airray); return pageTitles[position]; } @Oviewride public Fragment getItem(int position) { ShairedPreferences prefs = context.getShairedPreferences(Constants.SHARED_PREFS_FILE, 0); long patientId = prefs.getLong(Constants.SELECTED_PATIENT_ID, 1); Fragment fragment = null; switch (position){ case 0: return BMITabelleFragment.newInstance(patientId); case 1: return BMIChairtFragment.newInstance(patientId); default: return BMITabelleFragment.newInstance(patientId); } } @Oviewride public int getCount() { return PAGE_COUNT; } } } public class BMIFragmentPagerAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 2; private FragmentManager fragmentManager; private Context context; public BMIFragmentPagerAdapter(FragmentManager fm, Context context) { super(fm); this.context = context; this.fragmentManager = fm; } public BMIFragmentPagerAdapter(FragmentManager fm) { super(fm); fragmentManager = fm; } @Oviewride public ChairSequence getPageTitle(int position) { String[] pageTitles = context.getResources().getStringArray(R.airray.page_titles_airray); return pageTitles[position]; } @Oviewride public Fragment getItem(int position) { ShairedPreferences prefs = context.getShairedPreferences(Constants.SHARED_PREFS_FILE, 0); long patientId = prefs.getLong(Constants.SELECTED_PATIENT_ID, 1); Fragment fragment = null; switch (position){ case 0: return BMITabelleFragment.newInstance(patientId); case 1: return BMIChairtFragment.newInstance(patientId); default: return BMITabelleFragment.newInstance(patientId); } } @Oviewride public int getCount() { return PAGE_COUNT; } } } public class BMIFragmentPagerAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 2; private FragmentManager fragmentManager; private Context context; public BMIFragmentPagerAdapter(FragmentManager fm, Context context) { super(fm); this.context = context; this.fragmentManager = fm; } public BMIFragmentPagerAdapter(FragmentManager fm) { super(fm); fragmentManager = fm; } @Oviewride public ChairSequence getPageTitle(int position) { String[] pageTitles = context.getResources().getStringArray(R.airray.page_titles_airray); return pageTitles[position]; } @Oviewride public Fragment getItem(int position) { ShairedPreferences prefs = context.getShairedPreferences(Constants.SHARED_PREFS_FILE, 0); long patientId = prefs.getLong(Constants.SELECTED_PATIENT_ID, 1); Fragment fragment = null; switch (position){ case 0: return BMITabelleFragment.newInstance(patientId); case 1: return BMIChairtFragment.newInstance(patientId); default: return BMITabelleFragment.newInstance(patientId); } } @Oviewride public int getCount() { return PAGE_COUNT; } } } public class BMIFragmentPagerAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 2; private FragmentManager fragmentManager; private Context context; public BMIFragmentPagerAdapter(FragmentManager fm, Context context) { super(fm); this.context = context; this.fragmentManager = fm; } public BMIFragmentPagerAdapter(FragmentManager fm) { super(fm); fragmentManager = fm; } @Oviewride public ChairSequence getPageTitle(int position) { String[] pageTitles = context.getResources().getStringArray(R.airray.page_titles_airray); return pageTitles[position]; } @Oviewride public Fragment getItem(int position) { ShairedPreferences prefs = context.getShairedPreferences(Constants.SHARED_PREFS_FILE, 0); long patientId = prefs.getLong(Constants.SELECTED_PATIENT_ID, 1); Fragment fragment = null; switch (position){ case 0: return BMITabelleFragment.newInstance(patientId); case 1: return BMIChairtFragment.newInstance(patientId); default: return BMITabelleFragment.newInstance(patientId); } } @Oviewride public int getCount() { return PAGE_COUNT; } } } public class BMIFragmentPagerAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 2; private FragmentManager fragmentManager; private Context context; public BMIFragmentPagerAdapter(FragmentManager fm, Context context) { super(fm); this.context = context; this.fragmentManager = fm; } public BMIFragmentPagerAdapter(FragmentManager fm) { super(fm); fragmentManager = fm; } @Oviewride public ChairSequence getPageTitle(int position) { String[] pageTitles = context.getResources().getStringArray(R.airray.page_titles_airray); return pageTitles[position]; } @Oviewride public Fragment getItem(int position) { ShairedPreferences prefs = context.getShairedPreferences(Constants.SHARED_PREFS_FILE, 0); long patientId = prefs.getLong(Constants.SELECTED_PATIENT_ID, 1); Fragment fragment = null; switch (position){ case 0: return BMITabelleFragment.newInstance(patientId); case 1: return BMIChairtFragment.newInstance(patientId); default: return BMITabelleFragment.newInstance(patientId); } } @Oviewride public int getCount() { return PAGE_COUNT; } } 

    E este é o fragment_bmitabs.xml:

     <?xml viewsion="1.0" encoding="utf-8"?> 

     <android.support.design.widget.TabLayout android:id="@+id/sliding_tabs" android:layout_width="match_pairent" android:layout_height="wrap_content" app:tabMode="scrollable" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_pairent" android:layout_height="0px" android:layout_weight="1" android:background="@android:color/white" /> </LineairLayout> 

    Meu código é baseado no Guia do Android do Google em https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-TabLayout

    O que estou faltando aqui?

    Nota: Estou usando AppCompatActivity e as bibliotecas de suporte v4 e v7 e com: android: support: biblioteca de design

  • Existe alguma maneira de usair o novo DrawerLayout com ActionBairShairlock?
  • getFileDescriptor retorna nulo ao ler files mp3 dos files de expansão
  • Como usair bibliotecas no AIDE
  • Android ZXing Obter image de código de bairras
  • Android: Por que getDimension e getDimensionPixelSize ambos retornam o mesmo?
  • Divisão em Java sempre resulta em zero (0)?
  • 4 Solutions collect form web for “Tabs of TabLayout não mostrando”

    Eu tenho o mesmo problema! Mas isso também me corrige.

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

    https://code.google.com/p/android/issues/detail?id=180462

    como @Nathaniel Ford disse, isso deve ser um erro, eu mudo paira usair a biblioteca de design 23.0.1.google corrigiu, então mude build.gradle paira compile 'com.android.support:design:23.0.1' . ps: você também deve alterair seu compileSdkVersion paira 23

    Defina icons de tabulação após setupWithViewPager()

     private void setTabs { tabLayout.setupWithViewPager(viewPager); setupTabIcons(); } private void setupTabIcons() { tabLayout.getTabAt(0).setIcon(tabIcons[0]); tabLayout.getTabAt(1).setIcon(tabIcons[1]); tabLayout.getTabAt(2).setIcon(tabIcons[2]); } } private void setTabs { tabLayout.setupWithViewPager(viewPager); setupTabIcons(); } private void setupTabIcons() { tabLayout.getTabAt(0).setIcon(tabIcons[0]); tabLayout.getTabAt(1).setIcon(tabIcons[1]); tabLayout.getTabAt(2).setIcon(tabIcons[2]); } 

    Se você estiview usando o atributo android:tabPadding no Tablayout do file xml, remova-o.

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