Pregunta

I was reviewing the sample project for the support v4 package and stumbled upon something weird.:

R.id.fragment1 is used as a container for mTabHost during setup in FragmentNestingTabsSupport.java. Shouldn't it be an id corresponding to a view that is inside the FragmentActivity's view hierarchy?

All I see is that its view hierarchy is made of a single FragmentTabHost.

BTW: fragment1 is present inside fragment_hide_show_support.xml, which is part of a different demo.

¿Fue útil?

Solución

Shouldn't it be an id corresponding to a view that is inside the FragmentActivity's view hierarchy?

Not quite. If you look at the source code for the FragmentTabHost class you'll see that the FragmentTabHost will use the provided id only when the container, which will hold the content, doesn't already exist. This doesn't happen in that sample as the constructors of FragmentTabHost call a private method which has the task of building a layout(including the above content container) file if the FragmentTabHost doesn't contain a TabWidget.

So in that sample the FragmentTabHost doesn't contain a TabWidget which triggers a layout to be built. Having this built layout means the id provided to the setup() method is ignored. They might as well simply call mTabHost.setup(this, getSupportFragmentManager()); .

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top