Dans ce guide technique, nous explorerons comment implémenter une interface utilisateur basée sur des fragments dans une application Android, en utilisant un RadioGroup pour naviguer entre différentes sections. L'exemple montre comment initialiser, remplacer et gérer des fragments avec des transactions.
1. MainActivity.java
package com.example.fragmentapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.main_layout);
}
public void onNavigateToDemo(View view) {
Intent navIntent = new Intent(this, DemoActivity.class);
startActivity(navIntent);
}
}
2. DemoActivity.java
package com.example.fragmentapp;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup;
public class DemoActivity extends Activity {
private FragmentManager manager;
private FragmentTransaction transaction;
private RadioGroup menuGroup;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.demo_layout);
menuGroup = findViewById(R.id.navigation_group);
manager = getFragmentManager();
transaction = manager.beginTransaction();
transaction.add(R.id.content_container, new MessageFragment());
transaction.commit();
menuGroup.setOnCheckedChangeListener((group, checkedId) -> {
Fragment selectedFragment;
if (checkedId == R.id.option_messages) {
selectedFragment = new MessageFragment();
} else if (checkedId == R.id.option_contacts) {
selectedFragment = new ContactFragment();
} else {
selectedFragment = new ExploreFragment();
}
transaction = manager.beginTransaction();
transaction.replace(R.id.content_container, selectedFragment);
transaction.addToBackStack(null);
transaction.commit();
});
}
}
3. MessageFragment.java
package com.example.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MessageFragment extends Fragment {
private Button actionButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_message, container, false);
actionButton = rootView.findViewById(R.id.action_btn);
actionButton.setOnClickListener(v -> {
Toast.makeText(getActivity(), "Action effectuée", Toast.LENGTH_SHORT).show();
});
return rootView;
}
}
4. ContactFragment.java
package com.example.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ContactFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_contact, container, false);
}
}
5. ExploreFragment.java
package com.example.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExploreFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_explore, container, false);
}
}
6. main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exemple de FrameLayout"
android:onClick="onFrameExample"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Démo avec Fragments"
android:onClick="onNavigateToDemo"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Layout Grid"
android:onClick="onGridExample"
/>
</LinearLayout>
7. demo_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/navigation_bar" />
<LinearLayout
android:id="@+id/navigation_bar"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="@+id/navigation_group"
android:checkedButton="@id/option_messages"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/option_messages"
android:text="Messages"
android:drawableTop="@mipmap/ic_launcher"
android:clickable="true"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/option_contacts"
android:text="Contacts"
android:drawableTop="@mipmap/ic_launcher"
android:clickable="true"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/option_explore"
android:text="Explorer"
android:drawableTop="@mipmap/ic_launcher"
android:clickable="true"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/option_profile"
android:text="Profil"
android:drawableTop="@mipmap/ic_launcher"
android:clickable="true"
android:button="@null"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_above="@id/navigation_bar" />
</RelativeLayout>
8. fragment_message.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="Section Messages" />
<Button
android:id="@+id/action_btn"
android:text="Cliquer ici"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
9. frgament_contact.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="28sp"
android:textColor="@android:color/holo_red_dark"
android:text="Liste des Contacts" />
</LinearLayout>
10. fragment_explore.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="28sp"
android:textColor="@android:color/holo_green_light"
android:text="Découvrir du Contenu" />
</FrameLayout>