跳至主要内容

动态创建Fragment

MainActivity.kt --->

package com.example.fredric.dynamicfragment


import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import android.widget.Button
import android.widget.FrameLayout

class MainActivity : AppCompatActivity() , View.OnClickListener {
    override fun onClick(view: View?) {
        when(view?.id) {
            R.id.button ->{
                if(thisFragment) replaceFragment(RightFragment()) else replaceFragment(AnotherRightFragment())
            }
        }
    }

    var thisFragment = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var button = findViewById<Button>(R.id.button)
        button.setOnClickListener(this)
        replaceFragment(RightFragment())
    }

    fun replaceFragment(fragment: Fragment) {
        var fragmentManager = supportFragmentManager
        var transaction = fragmentManager.beginTransaction()
        var rightLayout = findViewById<FrameLayout>(R.id.right_layout)
        if(rightLayout.getTag() == null || rightLayout.getTag().equals("this")) {
            rightLayout.setTag("another")
            thisFragment = false
        }else {
            rightLayout.setTag("this")
            thisFragment = true
        }

        transaction.replace(R.id.right_layout,fragment)
        transaction.commit()

    }
}


LeftFragment.kt ->
package com.example.fredric.dynamicfragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class LeftFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        var view = inflater?.inflate(R.layout.left_fragment,container,false)
        return view!!
    }
}


RightFragment.kt -->

package com.example.fredric.dynamicfragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class RightFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        var view = inflater?.inflate(R.layout.right_fragment,container,false)
        return view!!
    }
}


AnotherRightFragment.kt --->

package com.example.fredric.dynamicfragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class AnotherRightFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        var view = inflater?.inflate(R.layout.another_right_fragment,container,false)
        return view!!
    }
}



activity_main.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"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.fredric.dynamicfragment.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    

    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>


手机模式:



























平板模式:



评论