FragmentTabHost no crea vista dentro de Fragmento en android
Tengo un problema al obtener la vista para cambiar en un tabhost – cuando selecciono una pestaña el contenido permanece en blanco.
Por lo que puedo decir, onCreateView
no está siendo llamado en el niño Fragmentos . onMenuCreate
funciona bien porque el menú cambia como se supone que.
- Cómo implementar pestañas personalizadas en Android como Instagram
- FindFragmentByTag - buscando fragmento en FragmentTabHost - always null
- Interfaz de Android
- Cómo crear un cuadro de diálogo con pestañas con Android que contiene fragmentos?
- El contenido de Tabhost no se muestra cuando actualizo android.support.v4
public class PatientTabFragment extends Fragment { private FragmentTabHost mTabHost; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mTabHost = new FragmentTabHost(getActivity()); mTabHost.setup(getActivity(), getChildFragmentManager()); mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Info"), NewPatientFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Notes"), NoteListFragment.class, null); return mTabHost; } @Override public void onDestroyView() { super.onDestroyView(); mTabHost = null; } }
- Cambiando dinámicamente los fragmentos dentro de un host de fichas de fragmentos?
- Reemplazar un fragmento a otro fragmento en el TabHost
- ¿Son la etiqueta y el icono mutuamente excluyentes en un tabHost TabSpec setIndicator?
- IllegalStateException en FragmentManager
- Fragmento superpuesto
- ¿Es posible llamar manualmente onCreateView en un fragmento?
Según los documentos :
TabHost especial que permite el uso de objetos Fragment para su contenido de tabulación. Al colocar esto en una jerarquía de vista, después de inflar la jerarquía debe llamar a setup (Context, FragmentManager, int) para completar la inicialización del host de tabulación.
(Énfasis mío)
Así que sugiero algo así:
public class PatientTabFragment extends Fragment { private FragmentTabHost mTabHost; private boolean createdTab = false; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mTabHost = new FragmentTabHost(getActivity()); mTabHost.setup(getActivity(), getChildFragmentManager()); mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Info"), NewPatientFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Notes"), NoteListFragment.class, null); return mTabHost; } public void onResume(){ if (!createdTab){ createdTab = true; mTabHost.setup(getActivity(), getActivity(). getSupportedFragmentManager()); } } @Override public void onDestroyView() { super.onDestroyView(); mTabHost = null; } }
Ahora podemos usar TabLayout y ViewPager hacer esas cosas. Esta es una buena guía para usarlo. Aquí está mi código:
viewPager=(NonSwipeableViewPager)view.findViewById(R.id.circleresdyn_viewpager); tabLayout=(TabLayout)view.findViewById(R.id.circleresdyn_tablayout); if (viewPager != null) { Adapter adapter = new Adapter(((AppCompatActivity)activity).getSupportFragmentManager()); ContentFragment con=new ContentFragment(); con.setArguments(bundleForFramgnet); MemberFragment memberFragment=new MemberFragment(); memberFragment.setArguments(bundleForFramgnet); CirResDynTileFragment cirResDynTileFragment=new CirResDynTileFragment(); cirResDynTileFragment.setArguments(bundleForFramgnet); adapter.addFragment(cirResDynTileFragment, "Tab1"); adapter.addFragment(con, "Tab2"); adapter.addFragment(memberFragment, "Tab3"); viewPager.setAdapter(adapter); viewPager.setOffscreenPageLimit(3); tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER); tabLayout.setupWithViewPager(viewPager); tabLayout.getTabAt(0).select(); }
Compruebe esta paz de código. Puede ayudarle:
import android.app.Fragment; public class Change_password extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.change_password, container,false); setTabs(); return rootView; } private void setTabs() { try { addTab("Airlines", R.drawable.tab_home, HomeActivity_bkp.class); addTab("Advance Search", R.drawable.tab_search, AdvanceSearchAcitivty.class); addTab("Booking", R.drawable.tab_home, Booking.class); addTab("Settings", R.drawable.tab_search, SettingAcitivty.class); } catch (Exception e) { Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); // TODO: handle exception } } private void addTab(String labelId, int drawableId, Class<?> c) { TabHost tabHost = getTabHost(); Intent intent = new Intent(this, c); TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); View tabIndicator = LayoutInflater.from(this).inflate( R.layout.tab_indicator, getTabWidget(), false); TextView title = (TextView) tabIndicator.findViewById(R.id.title); title.setText(labelId); ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); icon.setImageResource(drawableId); spec.setIndicator(tabIndicator); spec.setContent(intent); tabHost.addTab(spec); }