Fragment的增删改以及显示、隐藏操作

来源:互联网 发布:g900 支持 mac 编辑:程序博客网 时间:2024/05/17 01:18

这里写图片描述

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_manage_fragment"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingLeft="16dp"    android:paddingRight="16dp"    android:paddingBottom="16dp"    android:paddingTop="16dp"    tools:context="com.studio.fragmentdemo.ManageFragmentActivity">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="add"        android:text="Add Fragment"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="remove"        android:text="Remove Fragment"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="replace"        android:text="replace Fragment"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="hide"        android:text="hide Fragment"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="show"        android:text="show Fragment"/>    <FrameLayout        android:id="@+id/fl_container"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/></LinearLayout>
package com.studio.fragmentdemo;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class ManageFragmentActivity extends AppCompatActivity {    private FragmentManager fragmentManager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_manage_fragment);    }    public void add(View view){        fragmentManager = getSupportFragmentManager();        FragmentTransaction transaction = fragmentManager.beginTransaction();        FragmentTest fragmentTest = new FragmentTest();        transaction.add(R.id.fl_container,fragmentTest,"fragment");        transaction.commit();    }    public void remove(View view){       FragmentTransaction transaction= fragmentManager.beginTransaction();        transaction.remove(fragmentManager.findFragmentByTag("fragment"));        transaction.commit();    }    public void replace(View view){        FragmentTransaction transaction = fragmentManager.beginTransaction();        AnotherFragmentTest anotherFragmentTest = new AnotherFragmentTest();        transaction.replace(R.id.fl_container,anotherFragmentTest);        transaction.commit();    }    public void hide(View view){        FragmentTransaction transaction = fragmentManager.beginTransaction();        transaction.hide(fragmentManager.findFragmentByTag("fragment"));        transaction.commit();    }    public void show(View view){        FragmentTransaction transaction = fragmentManager.beginTransaction();        transaction.show(fragmentManager.findFragmentByTag("fragment"));        transaction.commit();    }}