作为一个Android开发人员,应该没有谁不知道Fragment!其重要性可想而知了!不多说,先上图
实例一(使用ListFragment和Fragment结合实现):
实例二(Fragment结合RadioButton)
Fragment的特征:
Fragment必须嵌入到Activity中使用。因此,即使Fragment拥有自己的生命周期,也会收到它所在的Activity的生命周期的控制。当Activity暂停时,Activity中所有的Fragment都会暂停;当Activity被销毁时,其中所有的Fragment将会被销毁;只有Activity处于活动状态时,程序员才可以通过方法独立地操作Fragment。
Fragment总是作为Activity界面组成部分。Fragment可调用getActivity()方法获取它所在的Activity。Activity可调用 FragmentManager的findFragmentById()或者findFragmentBuTag()方法来获取Fragment
在Activity运行过程中,可调用FragmentManager的add(),remove(),replace()方法动态添加,移除,修改。
一个Activity可以同时拥有多个Fragment,一个Fragment也可以被多个Activity复用。
- Fragment可以响应自己的输入事件,并拥有自己的生命周期,但生命周期被所属的Activity的生命周期控制。
Fragment常用知识点:
Fragment和所属Activity的通讯如何处理!
Activity向Fragment传递数据:在Activity中创建Bundle数据包,并调用Fragment的setArgument(Bundle bundle)方法将Bundle数据传递给Fragment.
Fragment向Activity传递数据:Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回掉接口,这样就能将Fragment的数据传给Activity了!
如何将Fragment添加到Activity中:
在布局文件中使用<fragment…/>元素添加Fragment,<fragment…/>元素的android:name属性指定Fragment的实现类
在Java代码中通过FragmentTransaction对象的add()方法来添加Fragment.
Activity获取它包含的Fragment:用Fragment的getActivity();
Fragment获取它所在的Activity:调用关联的FragmentMangager的findFragmentById(int id)或者fingFragmentByTag(String tag)方法即可获取Fragment。
Fragment管理
FragmentManager可以实现以下几方面的功能:
- 使用 findFragmentById(int id)或fingFragmentByTag(String tag)方法来获取指定的Fragment。
- 调用popBackStack()方法将Fragment从后台中弹出(模拟用户按下Back键)
- 调用addOnBackStackChangeListener()注册一个监听器,用于监听后台栈的变化。
Fragment事务
如果需要添加,删除,替换Fragment,则需要借助于FragmentTransaction对象,FragmentTransaction代表Activity对Fragment执行多个改变
- *
Fragment的生命周期
这里就不再手打赘述,直接从官网上复制最原始的解释给大家!多啰嗦一句,学习任何语言,官方文档一定要看。
The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:
onAttach(Activity) :called once the fragment is associated with its activity.
onCreate(Bundle): called to do initial creation of the fragment.
onCreateView(LayoutInflater, ViewGroup, Bundle) :creates and returns the view hierarchy associated with the fragment.
onActivityCreated(Bundle) :tells the fragment that its activity has completed its own Activity.onCreate().
onViewStateRestored(Bundle) :tells the fragment that all of the saved state of its view hierarchy has been restored.
onStart(): makes the fragment visible to the user (based on its containing activity being started).
onResume(): makes the fragment begin interacting with the user (based on its containing activity being resumed).
As a fragment is no longer being used, it goes through a reverse series of callbacks:
onPause(): fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
onStop(): fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
onDestroyView(): allows the fragment to clean up resources associated with its View.
onDestroy(): called to do final cleanup of the fragment’s state.
onDetach() :called immediately prior to the fragment no longer being associated with its activity.
创建Fragment
通常来说,创建Fragment只需要实现如下三个方法:
onCreat();
onCreatView();
onPause();
按需求可重写其他方法。
实例一:
MainActivity.java:用于对整个进程的控制
FoodContent:实体类
FoodListFragment:显示食品列表的Fragment
FoodDetailFragment:显示食品详细信息的Fragment
activity_main.xml:布局文件
……
MainActivity代码:
public class MainActivity extends AppCompatActivity implements FoodListFragment.Callbacks{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onItemSelected(Integer id) {
// 创建Bundle,准备向Fragment传入参数
Bundle arguments = new Bundle();
arguments.putInt(FoodDetailFragment.ITEM_ID, id);