Download - Fragments
Instructor: Rodrigo Iván Fonseca Daza
Muchas veces, los módulos que se muestran en un ‘Activity’ dependen, por ejemplo, del tamaño de la pantalla.
Si la pantalla es grande, se pueden mostrar dos módulos a la vez.
Si la pantalla es pequeña, los módulos se muestran en ‘Activities’ separados.
Módulo independiente que puede funcionar dentro de un ‘Activity’
Tiene su propio ciclo de vida y funcionalidades, lo que lo hace más fácil de reutilizar en diferentes contextos.
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment,
container, false);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.news.ArticleListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.news.ArticleReaderFragment" android:id="@+id/viewer" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
FragmentManager fragmentManager =
getFragmentManager()
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container,
fragment);
fragmentTransaction.commit();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
Determina si un Fragment se ha agregado en la descripción xml del layout de un ‘Activity’
Directorio en la carpeta res
Define los layouts para la aplicación en modo «Portrait»
getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE