Fragment的使用分为静态使用和动态使用。使用Fragment在导包时,有App包和V4包两种情况。这两种情况的显示效果都是一样的,只不过因为Fragment是在Android 3.0以上引入的,V4包下的Fragment可以兼容3.0以下的版本,而App包下的则不可以。值得注意的是,如果使用APP包下的Fragment,那么Activity是可以直接使用Activity的;而如果使用V4包下的Fragment,那么对应的Activity是不可以使用Activity的,必须使用FragmentActivity或者它的子类。现在分别使用下这两种Fragment:
一、静态使用(以V4包下的Fragment为例):
1.静态加载Fragment的步骤:
(1)定义Fragment的布局,就是Fragment显示内容的;
(2)自定义一个Fragment类,需要继承Fragment或者它的子类,重写onCreateView()方法;
(3)在需要加载Fragment的Activity对应的布局文件中添加Fragment标签。
2.代码如下:
(1)定义一个Fragment布局fragment_up:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#850099">
<TextView
android:text="这是Fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
(2).自定义一个Fragment类UpFragment:
package com.example.administrator.fragment_static;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017/3/21.
*/
public class UpFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View contentView=inflater.inflate(R.layout.fragment_up,container,false);
return contentView;
}
}
(3)在Activity中加载Fragment activity_main:
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.fragment_static.MainActivity"
android:orientation="vertical"
android:weightSum="2">
<fragment
android:name="com.example.administrator.fragment_static.UpFragment"
android:id="@+id/fragment_up"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
</LinearLayout>
MainActivity:
package com.example.administrator.fragment_static;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
二、动态使用(以App包下的为例):
1.动态加载Fragment的步骤:
(1)获得FragmentManager对象;(所有Fragment的管理方法都在FragmentManager中)
(2)获得FragmentTranscation对象;(对Fragment进行添加,移除,替换,以及执行其他动作)
(3)调用add方法或者replace方法加载Fragment对象;
(4)调用commit方法来提交事务。
2.代码如下:
(1)布局文件activity_main:fragment在动态加载时需要一个占位布局,而通常选择FragmentLayout作为Fragment的占位布局,因为它是最简单的一种布局,所有的内容都依次往左上角添加:
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.fragment_static.MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/btn_a"
android:text="添加FragmentA"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_b"
android:text="添加FragmentB"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="1"
android:id="@+id/fragment_content"
android:layout_width="wrap_content"
android:layout_height="0dp"></FrameLayout>
</LinearLayout>
(2)新建AFragment类和BFragment类分别继承Fragment,其对应的布局分别为fragment_a和fragment_b:
fragment_a:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="这是A"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
AFragment:
package com.example.administrator.fragment_dy;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017/3/21.
*/
public class AFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a,container,false);
}
}
B类似,把A改为B即可。
(3)在MainActivity中动态加载:
package com.example.administrator.fragment_dy;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
private Button aBtn,bBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.aBtn=(Button)super.findViewById(R.id.btn_a);
this.bBtn=(Button)super.findViewById(R.id.btn_b);
aBtn.setOnClickListener(this);
bBtn.setOnClickListener(this);
}
public void onClick(View view){
android.app.FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
switch (view.getId()){
case R.id.btn_a:
fragmentTransaction.replace(R.id.fragment_content,new AFragment());
break;
case R.id.btn_b:
fragmentTransaction.replace(R.id.fragment_content,new BFragment());
break;
}
fragmentTransaction.commit();
}
}