Fragment 動態加載例子

 activity_main.xml

<?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"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="100dp"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/btn_tom"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textSize="20dp"
            android:gravity="center"
            android:text="Tom"/>

        <Button
            android:id="@+id/btn_jane"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textSize="20dp"
            android:gravity="center"
            android:text="Jane"/>

    </LinearLayout>

    <FrameLayout
        android:id="@+id/people"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

</LinearLayout>

fragment_jane.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="#0f0"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="30sp"
        android:text="Jane" />
</LinearLayout>

fragment_tom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="#f00"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="30sp"
        android:text="Tom" />
</LinearLayout>

 FragmentJane.java

package com.jzl.fragmentdemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.app.Fragment;

public class FragmentJane extends Fragment {
    public FragmentJane()
    {

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_jane, container, false);
        return view;
    }
}
FragmentTom.java
package com.jzl.fragmentdemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.app.Fragment;



public class FragmentTom extends Fragment {
    public FragmentTom()
    {

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tom, container, false);
        return view;
    }
}
MainActivity.java
package com.jzl.fragmentdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //創建button對象
    private Button btnTom;
    private Button btnJane;
    //創建fragment對象
    private FragmentTom fragmentTom;
    private FragmentJane fragmentJane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //綁定ID
        bangID();
    }

    private void bangID() {
        btnTom = findViewById(R.id.btn_tom);
        btnJane = findViewById(R.id.btn_jane);
        btnTom.setOnClickListener(this);
        btnJane.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_tom:
                //判斷fragmentMan是否爲空,無則創建fragment對象
                if(fragmentTom ==null){
                    fragmentTom = new FragmentTom();
                }
                //創建FragmentManager對象
                FragmentManager manager = getFragmentManager();
                //創建FragmentTransaction事務對象
                FragmentTransaction fragmentTransaction =manager.beginTransaction();
                //使用replace(將要替換位置的i的,替換的頁面)方法實現頁面的替換
                fragmentTransaction.replace(R.id.people, fragmentTom);
                //提交事務
                fragmentTransaction.commit();
                break;
            case R.id.btn_jane:
                if(fragmentJane ==null){
                    fragmentJane = new FragmentJane();
                }
                FragmentManager  manager1 = getFragmentManager();
                FragmentTransaction fragmentTransaction1 =manager1.beginTransaction();
                fragmentTransaction1.replace(R.id.people, fragmentJane);
                fragmentTransaction1.commit();
                break;
            default:
                break;

        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章