Fragment的使用
其实碎片很简单,但是网上胡乱充数的博文太多了,以至于我们有时候觉得比较乱,今天就来简单讲解一下碎片的使用.
碎片的使用分为两种,静态添加碎片和动态添加碎片,我们就先来看一下静态添加碎片如何实现.
静态添加碎片
首先,先建两个Layout文件,这就是碎片的布局文件,大家可能也发现了,Android Studio里面可以直接快速建立碎片,就像Activity一样,但是这样会生成很多没用的代码,所以我们还是选择自己创建碎片布局.
两个布局都很简单,里面只有一个居中显示的TextView,下面贴一下代码.
第一个布局文件:fragment_first.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<? 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 = "@android:color/holo_blue_light" android:orientation = "vertical" > < TextView android:layout_width = "match_parent" android:layout_height = "match_parent" android:gravity = "center" android:text = "This is the first fragment!" /> </ LinearLayout > |
第二个布局文件fragment_second.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<? 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 = "@android:color/holo_green_light" android:orientation = "vertical" > < TextView android:layout_width = "match_parent" android:layout_height = "match_parent" android:gravity = "center" android:text = "This is the second fragment!" /> </ LinearLayout > |
现在布局文件建完了,我们该建立他们对应的Fragment了,也就是后台代码了。新建两个类,分别叫FirstFragment和SecondFragment,都继承于Fragment,需要注意一点,我们教程里面所使用的Fragment全都是android.support.v4.app.Fragment这个包下的,这样更有利于程序的兼容性.
贴一下两个类的代码,也很简单,只是重写了onCreateView方法来加载不同的布局文件.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class FirstFragment extends Fragment { private View view; //得到碎片对应的布局文件,方便后续使用 //记住一定要重写onCreateView方法 @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_first, container, false ); //得到对应的布局文件 return view; } } public class SecondFragment extends Fragment { private View view; //得到碎片对应的布局文件,方便后续使用 //记住一定要重写onCreateView方法 @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_second, container, false ); //得到对应的布局文件 return view; } } |
好,基本的工作我们做完了,现在我们用两个Activity展示如何静态添加碎片和动态添加碎片.
静态添加控件的话,需要使用fragment控件,指定其名称是你刚才创建的Fragment就可以,让我们来看一下.
先贴一下第一个Activity的布局:
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
|
<? 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:id = "@+id/activity_main" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = "me.worldskills.android.testfragment.MainActivity" > < fragment android:id = "@+id/main_firstfragment" android:name = "me.worldskills.android.testfragment.FirstFragment" android:layout_width = "match_parent" android:layout_height = "0dp" android:layout_weight = "1" ></ fragment > < fragment android:id = "@+id/main_secondfragment" android:name = "me.worldskills.android.testfragment.SecondFragment" android:layout_width = "match_parent" android:layout_height = "0dp" android:layout_weight = "1" ></ fragment > < Button android:id = "@+id/main_btnGoNext" android:layout_width = "match_parent" android:layout_height = "50dp" android:text = "Go to next page" android:textAllCaps = "false" /> </ LinearLayout > |
其中那个按钮是用来跳转到下一个界面的,也就是动态添加碎片案例的Activity,在这里可以忽略.
这里我们看见了,两个fragment分别指定name为FirstFragment和SecondFragment,也就是你刚才创建的两个Fragment,一定要记得加上包名.对了,还有一个问题,就是这样的话是没有预览的,如果想要预览,需要在fragment标签中加上一句代码:
Tools:layout="@layout/布局文件名称"
.
好了,静态添加碎片就完成了,什么?就这么简单,对啊...就这么简单.
动态添加碎片
动态添加碎片我们就不需要用fragment控件了,而是需要用个FrameLayout控件,这是为什么呢,首先我们都知道FrameLayout中的控件,都是从左上角开始显示,不用进行位置控制,动态添加碎片其实就是向容器里面动态添加碎片,而fragment控件只能用来静态绑定一个碎片.
先贴一下第二个Activity的布局:
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
|
<? 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:id = "@+id/activity_second" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" > < Button android:id = "@+id/second_btnLoadFirst" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "Load First!" android:textAllCaps = "false" /> < Button android:id = "@+id/second_btnLoadSecond" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "Load Second!" android:textAllCaps = "false" /> < FrameLayout android:id = "@+id/second_fl" android:layout_width = "match_parent" android:layout_height = "match_parent" > </ FrameLayout > </ LinearLayout > |
上面的两个按钮用来加载不同的碎片,而下面的FrameLayout就是碎片显示的容器.
废话不多说,贴代码:
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
|
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_second); //点击第一个按钮的时候加载第一个碎片 findViewById(R.id.second_btnLoadFirst).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.second_fl, new FirstFragment()); transaction.commit(); } }); //点击第二个按钮的时候加载第二个碎片 findViewById(R.id.second_btnLoadSecond).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { getSupportFragmentManager().beginTransaction().replace(R.id.second_fl, new SecondFragment()).commit(); //简写 } }); } } |
getSupportFragmentManager方法用来获得一个碎片管理器对象(使用这个方法的时候注意是android.support.v4.app包下的哦),然后通过这个方法开始一个碎片事物对象,这个对象比较关键,可以用来动态添加碎片,调用它的replace方法,会把指定容器里面的其他控件全部清除掉,然后添加新的碎片进去.在这里就是先把R.id.second_f1里面的控件清空,然后添加传入一个FirstFragment进去.
替换完之后一定要记得调用commit方法提交,要不然你的所有操作都不会生效,切记.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/Fill/p/7343855.html