Ein Fragment
in Android ist eine Subactivity
. Eine einzelne Activity
kann mehrere Fragments
enthalten. Wenn man also zum Beispiel eine Hauptseite mit Unterseiten braucht, dann muss man über Fragmente
nachdenken. Interessant ist, dass Fragmente
ihren eigenen Lebenszyklus haben und uns somit eine unabhängige Arbeitsweise ermöglichen, ohne dass wir uns so sehr auf Activities
verlassen müssen.
In diesem Tutorial wird die Verwendung von Fragmenten
anhand von einfachen How To Beispielen auf Basis von Kotlin Android gezeigt.
Beispiel 1: Kotlin Android - Zeige Fragment
in Activity
Die Activity
wird einen Button haben, der beim Anklicken ein Fragment
anzeigt. Hier ist das Demobild von dem, was erstellt wird:
Schritt 1: Abhängigkeiten
Für dieses Projekt werden keine Abhängigkeiten von Drittanbietern benötigt.
Schritt 2: Layouts entwerfen
Sie benötigen zwei Layouts: eines für das Fragment
und das andere für die Haupt- Aktivität
.
(a). Fragment.xml
Dies ist das Layout für das Fragment
. Es wird einfach einen Text enthalten:
<?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">
<TextView
android:id="@+id/txt_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:text="@string/str_fragment"
android:textColor="#2a2a2a"
android:layout_margin="10dp"
android:padding="10dp"/>
</LinearLayout>
(b). activity_main.xml`
Dies ist das Layout für die Hauptaktivität. Es enthält eine Schaltfläche und ein Rahmenlayout
. Wenn der Benutzer auf die Schaltfläche klickt, wird ein Fragment
initialisiert und im Framelayout
dargestellt:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.9"
android:text="@string/show"/>
<FrameLayout
android:id="@+id/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.1">
</FrameLayout>
</LinearLayout>
Schritt 3: Ein Fragment
erstellen
Der dritte Schritt besteht darin, das eigentliche Fragment
zu erstellen und sein Layout aufzublasen. Ein Fragment
wird durch Erweiterung der Klasse androidx.fragment.app.Fragment
erstellt. Es wird aufgeblasen, indem die Funktion "onCreateView()" überschrieben wird und ein View-Objekt zurückgegeben wird, das aus einem Layout für das Fragment aufgeblasen wurde.
Hier ist der vollständige Code:
Fragment_eins.kt
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment.*
class Fragment_one : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment,container,false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
txt_fragment.setOnClickListener {
Toast.makeText(activity,"text fragment",Toast.LENGTH_SHORT).show()
}
}
}
Schritt 4: Erstellen Sie MainActivity
Dies ist die Activity
, die das Fragment
beherbergen wird. Fragmente" existieren nicht alleine, sondern werden von "Aktivitäten" gehostet. Eine einzelne Aktivität
kann mehrere Fragmente
beherbergen.
Um ein Fragment
innerhalb einer Activity
zu zeigen, müssen Sie eine sogenannte Fragment
-Transaktion durchführen. Eine solche Transaktion kann das Hinzufügen eines Fragments
, das Entfernen eines Fragments
, das Ersetzen eines Fragments
usw. sein. In unserem Fall sind wir daran interessiert, unser Framelayout
durch unser Fragment
zu ersetzen. Hier ist der Code dazu:
fun showFragment(fragment: Fragment_one){
val fram = supportFragmentManager.beginTransaction()
fram.replace(R.id.fragment_main,fragment)
fram.commit()
}
Hier ist der vollständige Code für diese Activity
:
MainActivity.kt
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_show.setOnClickListener {
val fragment = arb.test.fragment.Fragment_one()
showFragment(fragment)
}
}
fun showFragment(fragment: Fragment_one){
val fram = supportFragmentManager.beginTransaction()
fram.replace(R.id.fragment_main,fragment)
fram.commit()
}
}
Run
Führen Sie nun das Projekt aus.
Referenz
Laden Sie den Code unten herunter:
No. | Link |
---|---|
1. | Download code |
2. | Folgen code author |