Fragment的基本用法

Fragment的介绍


Fragment是为了适配平板,大屏手机而生的!
fragment必须依赖于activity,但同时拥有自己的生命周期。
你可以把fragment当成activity的其中一部分,或者全部用fragment来填充activity布局!可以更加灵活的设计UI,适配不同尺寸的android设备!

如下图(来自官网),就好比是我们常见的新闻阅读客户端,在平板中有足够大的空间来同时容纳两个或者更多的fragment。Fragment A 可用于存放文章列表,而Fragment B 可以显示对应的文章内容。而手机受限于屏幕尺寸,可以在Activity A 容纳Fragment A,当用户点击文章标题后,跳转到Activity B 中的fragment B来显示相应的内容。
来自Android官方文档


1.静态加载:

首先,在Layout下创建两个xml


fm1.xml
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fm1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#3366ff">


</LinearLayout>


fm2.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fm2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ff0000">


</LinearLayout>


创建两个继承自Fragment的子类

fragment1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class fragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{

// TODO Auto-generated method stub

return inflater.inflate(R.layout.fm1, container, false);
}

}

fragment2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class fragment2 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{

// TODO Auto-generated method stub

return inflater.inflate(R.layout.fm2, container, false);
}

}


在activity_main.xml中引用之前创建的两个fragment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false" >


<fragment
android:id="@+id/f1"
android:name="com.example.simple.fragment1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />

<fragment
android:id="@+id/f2"
android:name="com.example.simple.fragment2"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />


</LinearLayout>


这样便完成了fragment的静态加载

Fragment的强大之处在于动态加载,可以更方便高效的完成复杂界面的动态布局!

2.动态加载:

首先,把activity_main.xml中fragment的引用去掉!

1
2
3
4
5
6
7
8
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >


</LinearLayout>

然后,在Mainactivty中修改,如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
fragment1 f1;
fragment2 f2;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
f1 = new fragment1();
f2 = new fragment2();
if (getWindowManager().getDefaultDisplay().getWidth() > getWindowManager()
.getDefaultDisplay().getHeight()) {
getFragmentManager().beginTransaction().replace(R.id.main, f1, null)
.commit();
} else if (getWindowManager().getDefaultDisplay().getWidth() < getWindowManager()
.getDefaultDisplay().getHeight()) {
getFragmentManager().beginTransaction()
.replace(R.id.main, f2, null).commit();
}

}
}


这样便完成了fragment的动态加载

当然了,这只是最基础的用法!