You are on page 1of 8

Les Fragments sous Android

Réalisé par: ELAOUTAR Ayoub


Définition

• Un fragment est un composant indépendant qui peut être utilisé dans une activité. Un
fragment encapsule des fonctionnalités qui le rendent facile à réutiliser dans une
activité ou dans une mise en page.

• Un fragment s'exécute dans le contexte d'une activité mais possède son propre cycle de
vie et typiquement, il possède une interface utilisateur. Il est aussi possible de définir
des fragments sans interface utilisateur, par exemple des fragments sans entête.

• Les fragments peuvent être ajoutés à une activité de manière statique ou dynamique.
Utilisation des fragments
Controler des Fragments

• Pour controller les fragments sur MainActivity nous sommes besoin d’un class qui
s’appelle “FragmentManager” utilisé pour créer des transactions pour ajouter,
supprimer ou remplacer des fragments.

• Et Pour la Transaction nous sommes besoin d’autre Class “FragmentTrasaction” , on


utilisent la méthode “beginTrasaction()” pour démarrer une série d'opérations
d'édition sur les fragments associés à ce gestionnaire de fragmentation.

• Il y a autre méthode aussi pour remplacer le fragment pricipale par le fragment qui
est été choisi par l’utilisateur, c’est la méthode « Replace »
La 1ere Interface
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
• Pour la 1er interface elle xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"

va contient deux Buttons


android:orientation="vertical"
android:layout_height="match_parent"
tools:context="fragmenttest4.orion.ma.fragmenttest.MainActivity">

et un Fragment <LinearLayout
android:layout_width="match_parent"

Principale qui va gérée android:layout_height="wrap_content"


android:orientation="horizontal">

les deux autres <Button


android:id="@+id/btn1"
android:onClick="valider"

Fragments. android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Fragment 1" />
<Button
android:id="@+id/btn2"
android:onClick="valider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Fragment 2" />

</LinearLayout>

<fragment
android:id="@+id/fragment"
android:name="fragmenttest4.orion.ma.fragmenttest.Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Ajouter un Fragment

• Pour ajouter un Fragment, on vas


cliquer sur New -> Fragment ->
Fragment (Blank).
• Il va construire deux fichier, un pour
JAVA et l’autre pour XMl
Controler les Fragments

public void valider(View view) {


• Pour controler les
Fragments d’aprés les if (view == bn1)
{
buttons sur MainActvity fragment = new Fragment1();
on doit taper ce code la. FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment,fragment);
ft.commit();

}
if (view == bn2)
{
fragment = new Fragment2();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment,fragment);
ft.commit();

}
Button sur un Fragment

• Pour coder un botton sur un public View onCreateView(LayoutInflater


inflater, ViewGroup container,
Fragment choisi comme la savedInstanceState) {
Bundle

photo suivante, si on clique // Inflate the layout for this


sur le Button par exemple le fragment
View view =
TextView va remplir par inflater.inflate(R.layout.fragment_fragm
ent1, container, false);
d’autre chose final TextView textView =
view.findViewById(R.id.text1);
bn1 = view.findViewById(R.id.btn3);
bn1.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("Hello to
the first Fragment"); }
});

return view;
}

You might also like