Android Fragment使用和切換 筆記

Demo下載
在這裏插入圖片描述
MainActivity代碼


import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Fragment1 fg1 =new Fragment1();
    private Fragment2 fg2 = new Fragment2();
    private TextView tv1;
    private TextView tv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SwithFragment(fg1,this,R.id.fg).commit();
        findViewById(R.id.fg1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SwithFragment(fg1,MainActivity.this,R.id.fg).commit();
            }
        });
        findViewById(R.id.fg2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SwithFragment(fg2,MainActivity.this,R.id.fg).commit();
            }
        });
    }
    Fragment cacheFragment;
    public FragmentTransaction SwithFragment(Fragment fragment, FragmentActivity fma, int idResources){
        FragmentManager fm= fma.getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (!fragment.isAdded()){//還沒添加 Fragment
            if (cacheFragment !=null){
                transaction.hide(cacheFragment);
            }
            transaction.add(idResources,fragment,fragment.getClass().getName());
        }else{//已經添加過了 Fragment
            transaction.hide(cacheFragment).show(fragment);
        }
        cacheFragment =fragment;
        return transaction;
    }
}

Fragment1 和 Fragment2 代碼

package com.manggai.fragmentswith;
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 androidx.fragment.app.Fragment;
public class Fragment1 extends Fragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1,container,false);
        return view;
    }
}

fragment1.xml 和 fragment2.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:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Fragment1"
        android:textSize="100px"
        ></TextView>
</LinearLayout>

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="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:id="@+id/fg"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_weight="1"
        android:layout_height="match_parent">
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80px"
        android:orientation="horizontal"
        >
        <TextView
            android:gravity="center"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="50px"
            android:id="@+id/fg1"
            android:textSize="15px"
            android:text="fg1"
            android:layout_weight="1"
            ></TextView>
        <TextView
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:layout_height="50px"
            android:id="@+id/fg2"
            android:textSize="15px"
            android:text="fg2"
            ></TextView>
    </LinearLayout>
</LinearLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章