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(); } }
相关文章推荐
- 获取网络状态以及Fragment的隐藏和显示使用
- Android Fragment管理类,操作Fragment的增加、删除、替换、隐藏、显示等
- android fragment动态显示隐藏
- 安卓 Fragment显示和隐藏 与 onResume
- js/jquery操作div显示或隐藏
- jQuery学习_动作效果(隐藏、显示、切换,滑动,淡入淡出,以及动画)
- Win7系统如何显示隐藏文件的具体操作步骤
- jQuery 显示 隐藏 操作
- C#中对数据库的基本操作(增删改以及调用存储过程)
- android隐藏以及显示软键盘
- 夜间模式、全屏,以及虚拟按键的动态隐藏和显示
- MySQL优化系列(一)--库与表基本操作以及数据增删改
- easyUI tabs控件显示与隐藏以及跳转
- ListView控制Fragment的显示与隐藏
- Jquery显示、隐藏元素以及添加删除样式
- Mac显示或不显示隐藏文件操作命令
- .net添加前端样式和JS添加标签样式、以及div/a 标签的显示和隐藏
- Fragment隐藏显示与DrawerLayout侧拉 Banner与PullToRefreshListView 和 Banner与xlistview分类加载数据
- [置顶] FloatingActionButton属性、用法,以及解析并解决sdk25以上只隐藏不显示的问题
- android 命令隐藏导航栏以及显示电量