您的位置:首页 > 其它

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

2017-08-01 17:23 218 查看


<?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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: