您的位置:首页 > 移动开发 > Android开发

Android - Fragment(二)加载Fragment

2015-08-05 22:03 507 查看
Fragment加载方法

加载方法有两种,在xml文件中注册,或者是在Java代码中加载。

xml中注册

例如在fragment_demo.xml中定义

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
android:id="@+id/main_fragment_up"
android:name="com.rust.fragment.FirstFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<fragment
android:id="@+id/main_fragment_bottom"
android:name="com.rust.fragment.SecondFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

</LinearLayout>


com.rust.fragment.SecondFragment 就是Fragment子类

在 SecondFragment.java 里复写onCreateView方法,并返回定义好的view

activity中直接加载即可

setContentView(R.layout.fragment_demo);

Java代码中加载

① 准备好Fragment xml布局文件

② 新建一个类,继承自Fragment;在这个类中找到Fragment布局文件

③ 在Activity中使用FragmentManager来操作Fragment

④ 别忘了commit

先自定义一个布局文件 fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0011ff" >

<!-- <Button
android:id="@+id/btn_fragment1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_fragment1"
android:textSize="16sp"
/>
<EditText
/> -->

</LinearLayout>


新建一个类 FirstFragment.java ,继承自Fragment。复写onCreateView方法。在onCreateView方法中,可以操作Fragment上的控件。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_first, container,false);

//    fragment_first是自定义好的布局

//    如果此Fragment上放了控件,比如Button,Edittext等。可以在这里定义动作

btn_fragment1_send = (Button) rootView.findViewById(R.id.btn_fragment1_1);

//...
return rootView;
}


准备一个位置给Fragment,比如在 activity_main.xml 中用Framelayout来占位。

<FrameLayout

android:id="@+id/layout_container1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4" >

</FrameLayout>


在 MainActivity.java 里,先获得FragmentManager,得到FragmentTransaction。Fragment的添加删除等操作由FragmentTransaction来完成。

f1 = new FirstFragment();    //    获取实例

f2 = new SecondFragment();    //

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

fragmentTransaction.add(R.id.layout_container1,f1);    //    添加

fragmentTransaction.replace(R.id.layout_container1,f1);    //    替换

// 或者也可以写成

fragmentTransaction.replace(R.id.layout_container1,new FirstFragment());

//                fragmentTransaction.addToBackStack(null);    //添加到返回栈,这样按返回键的时候能返回已添加的fragment

fragmentTransaction.commit();    //别忘了commit

//    移除操作 getFragmentManager().beginTransaction().remove(f1).commit();


相比与xml中注册,代码加载更为灵活一些。个人较为喜欢动态加载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: