Un Fragment è un componente Android che può essere inserito in un file Activity. Ha un proprio ciclo di vita e può gestire i propri eventi di input, poiché definisce e gestisce il proprio layout.
Utilizzando FragmentManager e FragmentTransaction possiamo interagire con i frammenti che si trovano all’interno del nostro layout.
In questo esempio andiamo a creare due frammenti e un pulsante per visualizzare il rispettivo frammento quando si fa clic:
Activity_main.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:gravity=”center”
android:orientation=”vertical”>
<Button
android:id=”@+id/button”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Change Fragment”/>
<FrameLayout
android:id=”@+id/frameLayout”
android:layout_width=”match_parent”
android:layout_height=”match_parent”/>
</LinearLayout>
Fragment_1.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”First Fragment”
android:textSize=”50sp”/>
</LinearLayout>
Fragment_2.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Second Fragment”
android:textSize=”50sp”/>
</LinearLayout>
MainActivity.java
package com.example.changeFragment.activities;
import android.app.Activity;
import android.os.Bundle;
import com.example.changeFragment.R;
import android.widget.*;
import android.view.*;
import android.app.*;
public class MainActivity extends Activity {
Button button;
boolean result=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!result){
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayout,new Fragment1()).commit();
button.setText(“Fragment 1”);
result =true;
}else {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayout,new Fragment2()).commit();
button.setText(“Fragment 2”);
result = false;
}
}
});
}
}
Fragment1.java
package com.example.changeFragment.activities;
import android.view.*;
import android.os.*;
import com.example.changeFragment.R;
import android.app.*;
public class Fragment1 extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_1, container, false);
}
Fragment2.java
package com.example.changeFragment.activities;
import android.view.*;
import android.os.*;
import android.app.*;
import com.example.changeFragment.R;
public class Fragment2 extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_2, container, false);
}
}