android-fragments Tutorial
Erste Schritte mit Android-Fragmenten
Suche…
Bemerkungen
Fragmente sind sehr wichtige Komponenten der Benutzeroberfläche in Android-Apps. Sie wurden zuerst in Android 3.0 (Honeycomb) API eingeführt.
Design-Paradigma von Fragmenten verstehen
Fragmente wurden eingeführt, um in erster Linie modulare und flexible Benutzeroberflächen auf Großbildschirmgeräten wie Tablets zu unterstützen.
Fragmente werden von einer Aktivität verwaltet. Normalerweise repräsentiert jedes Fragment einen Teil eines Bildschirms. In einer Aktivität kann es mehr als ein Fragment geben. Fragmente können auch als Unteraktivitäten bezeichnet werden . Wenn Sie ein Fragment als Teil Ihres Aktivitätslayouts hinzufügen, befindet es sich in einer ViewGroup innerhalb der Ansichtshierarchie der Aktivität, und das Fragment definiert ein eigenes Ansichtslayout.
LEBENSZYKLUS
Fragmente haben ebenso wie Aktivitäten einen Lebenszyklus. Ein Fragment wird für folgende Ereignisse benachrichtigt.
- An Aktivität gebunden werden - onAttach (Aktivität)
- Fragment erstellen - onCreate (Bundle)
- View- onCreateView erstellen (LayoutInflater, ViewGroup, Bundle)
- Aktivitätserstellung - onActivityCreated (Bundle)
- Ansichtsstatus wiederhergestellt - onViewStateRestored (Bundle)
- Für den Benutzer sichtbar gemacht - onStart ()
- Beginn der Benutzerinteraktion - onResume ()
- Pause der Benutzerinteraktion - onPause ()
- Für den Benutzer unsichtbar gemacht - onStop ()
- On-View-Zerstörung - onDestroyView ()
- Fragment zerstören - onDestroy ()
- Losgelöst von einer Aktivität - onDetach ()
Als Programmierer sollten Sie verschiedene Lebenszyklus-Callback-Methoden überschreiben. In der Regel implementieren wir die Methoden onCreate (), onCreateView () und onPause ().
Unterklassen von Fragment
- DialogFragment - Zur Anzeige des schwebenden Dialogs
- ListFragment - Zum Anzeigen der Liste der Elemente
- PreferenceFragment - Nützlich zum Erstellen von Einstellungen
Verweise
Fragmente hinzufügen
Fragment statisch hinzufügen
Datei: activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragmentexample.Fragment2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentexample.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
Datei: fragment1.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"
android:background="#00ff00"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment frist"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Datei: fragment2.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"
android:background="#0000ff"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Fragment"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Datei: MainActivity.java
package com.example.fragmentexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Datei: Fragment1.java
package com.example.fragmentexample;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment1,container, false);
}
}
Datei: Fragment2.java
package com.example.fragmentexample;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment2,container, false);
}
}
Fragment dynamisch hinzufügen
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@+id/container1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<FrameLayout
android:id="@+id/container2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
FrameLayout fungiert als Fragmentcontainer.
MainActivity-Klasse
Datei: MainActivity.java
package com.example.fragmentexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadFragment(this, R.id.container1,new Fragment1(),"fragment1");
loadFragment(this, R.id.container2,new Fragment2(),"fragment2");
}
public static void loadFragment(Activity activity, int containerId, Fragment fragment, String tag)
{
activity.getSupportFragmentManager().beginTransaction().
replace(containerId, fragment,tag).commitAllowingStateLoss();
}
}