管理Fragment(1)

前言:follow your heart,be your own king

相关文章:

1、《Fragment详解之一——概述》2、《Fragment详解之二——基本使用方法》3、《Fragment详解之三——管理Fragment(1)》4、《Fragment详解之四——管理Fragment(2)》5、《Fragment详解之五——Fragment间参数传递》6、《Fragment详解之六——如何监听fragment中的回退事件与怎样保存fragment状态》

前面给大家稍微看了要怎么使用fragment,在上篇中,我们也初步接触到了add,replace这些fragment操作的函数,下面就再详细讲讲如何管理Fragment页面吧。

一、概述1、FragmentManager

要管理activity中的fragments,你就需要使用FragmentManager。通过getFragmentManager()或getSupportFragmentManager()获得 常用的方法有:

manager.findFragmentById(); //根据ID来找到对应的Fragment实例,主要用在静态添加fragment的布局中,因为静态添加的fragment才会有IDmanager.findFragmentByTag();//根据TAG找到对应的Fragment实例,主要用于在动态添加的fragment中,根据TAG来找到fragment实例manager.getFragments();//获取所有被ADD进Activity中的Fragment2、FragmentTransaction一般用来对当前的Fragment进行管理,包括add,replace,remove;常用的针对Fragment的方法有://将一个fragment实例添加到Activity的最上层add(int containerViewId, Fragment fragment, String tag);//将一个fragment实例从Activity的fragment队列中删除remove(Fragment fragment);//替换containerViewId中的fragment实例,注意,它首先把containerViewId中所有fragment删除,然后再add进去当前的fragmentreplace(int containerViewId, Fragment fragment);还有hide()、show()、detach()、attach()这些函数,我们下篇再讲,这节先对Fragment的用法有一个初步了解;二、add()、replace()、remove()使用方法示例

下面就通过例子来看看以上几个函数的使用方法吧。 效果图如下:

那现在我们从头开始构建这个工程:

1、新建两个fragment1.xml 和 fragment2.xml:

从效果图中也可以看出,这两个XML什么都没有,只是通过背景色和文字来区别当前是哪个Fragment的XML布局文件而已,他们的布局代码如下:

fragment1.xml:<LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ff00f0"android:orientation="vertical" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="This is fragment 1"android:textColor="#000000"android:textSize="25sp" /></LinearLayout>fragment2.xml:<LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ffff00"android:orientation="vertical" ><TextViewandroid:id="@+id/fragment2_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="This is fragment 2"android:textColor="#000000"android:textSize="25sp" /></LinearLayout>2、建立对应的Fragment类:Fragment1和Fragment2Fragment1:import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment1, container, false);}}与上一篇一样,也只是在onCreateView()时返回对应的布局。同样,Fragment2的定义如下:import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment2 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment2, container, false);}}3、MainActivity的布局从上面的的效果图中也可以看出大概的布局,首先是三个Button,最下方是一个FrameLayout布局,它是用来做为container动态盛装fragment的;它就像是一个占位符,你设置多大,它其中的fragment就最大能有多大。记住,fragment也是Activity中的一个普通控件而已,只不过它可以像Activity一样用于显示的同时还能用来盛装其它控件!作为fragment的容器,即可以用FrameLayout也可以用LinearLayout或者RelativeLayout,都是一样的。activity_main.xml的布局代码如下:<LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/btn_add_frag1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="ADD Fragment1" /><Buttonandroid:id="@+id/btn_add_frag2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="ADD Fragment2" /><Buttonandroid:id="@+id/btn_remove_frag2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Remove Fragment2" /><Buttonandroid:id="@+id/btn_repalce_frag1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="replace Fragment1" /><FrameLayoutandroid:id="@+id/fragment_container"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>4、MainActivity的实现:(1)首先,先写一个添加fragment到Activity中的函数:private void addFragment(Fragment fragment, String tag) {FragmentManager manager = getSupportFragmentManager();FragmentTransaction transaction = manager.beginTransaction();transaction.add(R.id.fragment_container, fragment, tag);transaction.commit();}累死累活不说,走马观花反而少了真实体验,

管理Fragment(1)

相关文章:

你感兴趣的文章:

标签云: