Fragment-两种使用方式
这篇我们就用实例来看看我们在代码中如何使用Fragment
一:静态添加Fragment
新建一个项目,添加两个Fragment的布局文件fragment_title,fragment_content
<?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:background="#44444433"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是微信"/>
</LinearLayout>
<?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:background="#44994433"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是Fragment内容"/>
</LinearLayout>
然后创建两个class继承自Fragment,这里面导入的包是android.app.Fragment,而不是V4包,对于这两个包的区别后面我会贴出。
package com.jkxy.fragmentdemo;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by cuishuang on 16/7/29.
*/
public class TitleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_title,container,false);
}
}
package com.jkxy.fragmentdemo;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by cuishuang on 16/7/29.
*/
public class ContentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_content,container,false);
}
}
最后是MainActivity,这里面没加其他代码这里就不在贴出了
运行效果:

二:动态添加Fragment
你已经学会了如何在XML中使用Fragment,但是这仅仅是Fragment最简单的功能而已。Fragment真正的强大之处在于可以动态地添加到Activity当中,因此这也是你必须要掌握的东西。当你学会了在程序运行时向Activity添加Fragment,程序的界面就可以定制的更加多样化。下面我们立刻来看看,如何动态添加Fragment。
还是在上一节代码的基础上修改,打开activity_main.xml,将其中代码全部删除,改成下面的样子:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.jkxy.fragmentdemo.MainActivity">
<Button
android:id="@+id/btn_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示第一个Fragment"/>
<Button
android:id="@+id/btn_fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示第二个Fragment"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
主界面上有两个按钮和一个FrameLayout布局。这两个按钮分别用来在这个FrameLayout加载Fragment1和Fragment2的实例。效果如下:
其它代码都没有动,主要的是在MainActivity里,点击这两个按钮时做的处理:
package com.jkxy.fragmentdemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void initView(){
findViewById(R.id.btn_fragment1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment1 frgment1 = new Fragment1();
transaction.add(R.id.fragment_container,frgment1);
transaction.commit();
}
});
findViewById(R.id.btn_fragment2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment2 frgment2 = new Fragment2();
transaction.add(R.id.fragment_container,frgment2);
transaction.commit();
}
});
}
}
看上面的代码很容易明白,在点击按钮时都做了类似的操作:
FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); Fragment2 frgment2 = new Fragment2(); transaction.add(R.id.fragment_container,frgment2); transaction.commit();
动态添加Fragment主要分为4步:
- 1.获取到FragmentManager,在V4包中通过getSupportFragmentManager,在系统中原生的Fragment是通过getFragmentManager获得的。
- 2.开启一个事务,通过调用beginTransaction方法开启。
- 3.向容器内加入Fragment,一般使用add或者replace方法实现,需要传入容器的id和Fragment的实例。
- 4.提交事务,调用commit方法提交。
app包中的fragment和v4包中的fragment的使用的区别
1、尽量不要用app包中的fragment,因为这个是在3.0之后才有的,支持的版本太高,在低版本中是是用不了的
2、android.support.v4.app.Fragment:可以兼容到1.6的版本,
3、关于这两个fragment使用<fragment>标签的问题
(1)app.fragment和v4.fragment都是可以使用<fragment>标签的
只是在在使用的时候如果是app.fragment则没有什么特殊的地方继承Activity即可。
(2)当v4.fragment使用<fragment>标签的时候就要特别注意了:
当这个Activity的布局中有<fragment>标签的时候,这个Activity必须继承
FragmentActivity,否则就会报错
08-27 08:25:04.946: E/AndroidRuntime(9839): Caused by: java.lang.ClassCastException: com.example.android_fragment_bottom.fragments.TopBarFragment cannot be cast to android.app.Fragment
此时如果不卜继成FragmentActivity的话 编译系统会把<fragment>认为是app包中的Fragment来处理。但是此时我们导入的是v4包中的Fragment
Android官方文档中的Fragment的例子就是以app包中的Fragment来讲解的。
(3)app包中关于Fragment的类和方法在V4包中都是有相应的对应的

浙公网安备 33010602011771号