Utilizzare i Fragmens in Android e Java

Mattepuffo's logo
Utilizzare i Fragmens in Android e Java

Utilizzare i Fragmens in Android e Java

I Fragments in Android possono essere molto utili in diverse occasioni.

In pratica rappresentano porzioni riutilizzabili di codice; ogni Fragment ha il proprio layout e il proprio ciclo di vita.

Oggi vediamo un esempio del loro utilizzo in Java.

Nella vostra app create un Fragment del genere:

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class PrimoFragment extends Fragment {

    private View view;
    private Button fbtnPrimo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_primo, container, false);
        fbtnPrimo = view.findViewById(R.id.btnPrimo);
        fbtnPrimo.setOnClickListener(v -> Toast.makeText(getActivity(), "Ciao dal primo!", Toast.LENGTH_LONG).show());
        return view;
    }
}

E questo il layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mp.javatest.PrimoFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:text="Primo Fragment"
        android:textColor="@color/black"
        android:textSize="25sp" />

    <Button
        android:id="@+id/btnPrimo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="Clicca"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold" />
</RelativeLayout>

Poi create un altro Fragment:

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SecondoFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_secondo, container, false);
    }
}

E questo il layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondoFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

Il secondo lo abbiamo fatto più semplice.

A questo punto dobbiamo vedere la nostra Activity:

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button pBtn;
    private Button sBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pBtn = findViewById(R.id.pBtn);
        sBtn = findViewById(R.id.sBtn);

        pBtn.setOnClickListener(v -> loadFragment(new PrimoFragment()));
        sBtn.setOnClickListener(v -> loadFragment(new SecondoFragment()));
    }

    private void loadFragment(Fragment fragment) {
        FragmentManager manager = MainActivity.this.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.replace(R.id.frameLayout, fragment);
        fragmentTransaction.commit();
    }

}

E questo il layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/pBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Primo Fragment"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <Button
        android:id="@+id/sBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Secondo Fragment"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" />
</LinearLayout>

Direi che è tutto; lanciate la app per vedere il comportamento!

Enjoy!


Condividi

Commentami!