欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android Fragment使用和切换 笔记

程序员文章站 2022-05-14 21:08:10
...

Demo下载
Android Fragment使用和切换 笔记
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>
相关标签: Android Fragment