天天看點

Fragment的動态添加-android-kotlin

Fragment是一種可以嵌入到Activity當中的UI片段

可以使用他來為app适配大屏平闆

參考資料:《第一行代碼》

喜歡就堅持吧!

文章目錄

      • 效果
      • 具體實作
        • 左Fragment布局`left_layout.xml`
        • 右Fragment布局(切換前)`right_layout.xml`
        • 右Fragment布局(切換後)`another_right_layout.xml`
        • 主布局`activity_main.xml`
        • 邏輯實作`MainActivity.kt`

效果

Fragment的動态添加-android-kotlin
與之前寫的界面的差別,就是這個是由一左一右兩個Fragment構成的,能更好的的适配平闆裝置.

具體實作

左Fragment布局

left_layout.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">
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/go"/>
</LinearLayout>

           

右Fragment布局(切換前)

right_layout.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:background="#126BE6">
</LinearLayout>

           

右Fragment布局(切換後)

another_right_layout.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:background="#E61912">
</LinearLayout>

           

主布局

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <fragment
            android:id="@+id/leftFragment"
            android:name="com.example.fragmenttest2.LeftFragment"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"/>
        <FrameLayout
            android:id="@+id/rightLayout"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
           

邏輯實作

MainActivity.kt

package com.example.fragmenttest2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.left_fragment.*

//載入左布局檔案
class LeftFragment():Fragment(){
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.left_fragment,container,false)
    }
}
//載入右布局檔案(切換前)
class RightFragment():Fragment(){
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.right_fragment,container,false)
    }
}
//載入右布局檔案(切換後)
class AnotherRightFragment():Fragment(){
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.another_right_fragment,container,false)
    }
}
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        replaceFragment(RightFragment())    //預設情況下加載切換前的右布局檔案
        button.setOnClickListener{
            replaceFragment(AnotherRightFragment()) //點選後動态切換為左布局檔案
        }
    }
    private fun replaceFragment(fragment: Fragment){
        val fragmentManager = supportFragmentManager //擷取FragmentManager
        val transaction = fragmentManager.beginTransaction() //開啟一個事務
        transaction.replace(R.id.rightLayout,fragment)  //替換容器内的fragment
        transaction.addToBackStack(null)    //傳回棧,實作按下back鍵傳回上一個fragment
        transaction.commit()    //送出事務
    }
}