Kotlinde Activity içinde Fragment Kullanımı
Önce bir proje oluşturuz. Kurduğumuz projede new seçeneğiyle iki yeni fragment ekleriz. Burda Blank Fragment türünü seçeriz.
Blank Fragmentlere istediğin gibi isim verebilirsin. Ancak ben "Fragment_One" ve " Fragment_two" isimlerini verdim.
Activity Main in xml sini / layoutunu silerek yerine aşağıdaki kodu yapıştıralım.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_marginTop="0dp"
android:layout_alignParentTop="true"
android:gravity="center"
android:id="@+id/linearLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonFragmentOne"
android:text="Fragment One"
android:textSize="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonFragmentTwo"
android:text="Fragment Two"
android:textSize="20dp"/>
</LinearLayout>
<FrameLayout
android:id="@+id/frameLayoutForFragments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/linearLayout"
android:orientation="horizontal">
</FrameLayout>
</RelativeLayout>
Res dosyalarından stringsi seçip aşağıdaki kodu yapıştıralım.<resources>
<string name="app_name">Fragment Example</string>
<string name="fragment_one_title">Birinci Fragment</string>
<string name="fragment_two_title">İkinci Fragment</string>
</resources>Fragment_One 'ın içini silip yerine aşağıdaki kodu yapıştıralım.
<?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=".Fragment_One">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/fragment_one_title"/>
<TextView
android:id="@+id/textView"
android:layout_width="113dp"
android:layout_height="126dp"
android:layout_gravity="center"
android:text="1"
android:textSize="100dp"
android:textStyle="bold" />
</FrameLayout><?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=".Fragment_two">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:text="@string/fragment_two_title"/>
<TextView
android:id="@+id/textView"
android:layout_width="347dp"
android:layout_height="273dp"
android:layout_gravity="center"
android:text="2"
android:textSize="100dp"
android:textStyle="bold" />
</FrameLayout>class MainActivity : AppCompatActivity() override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
val buttonFragmentOne: Button = findViewById(R.id.buttonFragmentOne)
val buttonFragmentTwo: Button = findViewById(R.id.buttonFragmentTwo)
buttonFragmentOne.setOnClickListener {
callFragment(Fragment_One())
}
buttonFragmentTwo.setOnClickListener {
callFragment(Fragment_two())
}
}
fun callFragment(fragment: Fragment) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.frameLayoutForFragments, fragment)
fragmentTransaction.commit()
}
}
Yorumlar
Yorum Gönder