四大组件之Activity的瘦身版Fragment(片段)
来源:互联网 发布:小米6相机优化 编辑:程序博客网 时间:2024/05/17 16:11
Fragment简介
- Fragment是Google在3.0后专为平板新出的.
- Fragment就是mini型的Activity,拥有自己的生命周期,响应属于自己的输入事件.
- 必须被嵌入到一个Activity中,其生命周期直接受其宿主activity的生命周期影响.
- 当一个Activity正在运行时,可以独立地操作每一个Fragment,比如添加或删除它们.
- Fragment可以定义自己的布局、生命周期回调方法,因此可以重用到多个Activity中.
静态添加Fragment(不灵活,推荐动态)
先来两个Fragment的布局
Left
<?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:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="左边的按钮" /></LinearLayout>
public class LeftFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_left, container, false); return view; }}
Right
<?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:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="右边的文字" /></LinearLayout>
public class RightFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_right, container, false); return view; }}
MainActivity中加载两个Fragment
xml布局中通过
<fragment>
标签静态添加即可必须要为每个
<fragment>
标签加上id,否则编译不通过!<?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"> <fragment android:id="@+id/fg_left" android:name="com.catface.demos.fragment.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fg_right" android:name="com.catface.demos.fragment.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /></LinearLayout>
动态创建Fragment:使用向下兼容的API
为Fragment定义一个布局.
定义类继承Fragment,重写onCreateView(),初始化Fragment布局.
MainActivity继承Activity
a) 创建定义完成的Fragment对象.
b) 获取Fragment管理器:
getFragmentManager()
.c) 开启事务:
beginTransaction()
.d) 通过
replace()
显示Fragment对象至指定容器中.ps)
addToBackStack(null)
来模拟栈效果.e) 提交:
commit()
.public void replace(View v) { MyFragment myFragment = new MyFragment(); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.framelayout, myFragment); transaction.addToBackStack(null); /** Fragment中模拟返回栈 */ transaction.commit();}
replace()
时可以带个标记,这样可以使用Fragment管理器通过标记拿到Fragment对象.
Fragment的向下兼容(API为support-v4包)
MainActivity继承FragmentActivity
获取Fragment管理器:
getSupportFragmentManager()
上面介绍的是动态创建Fragment,静态创建就两个标签搞定fragment + name
Fragment的生命周期
onAtach()
:Fragment与Activity建立关联时调用.onCreate()
:创建Fragment.onCreateView()
:Fragment加载布局时调用.onActivityCreated()
:当Activity中的onCreate方法执行完成后调用.onStart()
:可见,不可交互.onResume()
:可见,可交互.onPause()
:部分可见,不可交互.onStop()
:不可见,不可交互.onDestroyView()
:Fragment中的布局被移除时调用.onDestroy()
:Fragment销毁时回调.onDetach()
:Fragment与Activity解除关联时回调.开新Fragment:
onAtach()、onCreate()、onCreateView()、onActivityCreated()、onStart()、onResume()
.点击HOME键:
onPause()、onStop()
.重新进入Fragment:
onStart()、onResume()
.点击BACK键:
onPause()、onStop()、onDestroyView()、onDestroy()、onDetach()
.
Fragment与Activity传递数据
Fragment获取Activity中的数据:getActivity().findViewById()拿到Activity布局中的组件并设置数据.
Activity获取Fragment中的数据:getSupportFragmentManager().findFragmentByTag()获取Fragment对象.
Fragment之间传递数据:getFragmentManager().findFragmentById()并设置数据.
以上。如有错误和疑问,欢迎指正提出。 catface.wyh@gmail.com
- 四大组件之Activity的瘦身版Fragment(片段)
- 四大组件--Activity(Fragment)
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之 Activity
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之Activity
- 四大组件之Activity
- Cmake MySQL5.6 编译安装
- IOS页面自动布局 之 NSLayoutConstraint基础篇
- java编程中怎么控制小数点后的位数?
- Tomcat下载使用
- Java SE | SSM 日期类型的转换 由String 转换为Date
- 四大组件之Activity的瘦身版Fragment(片段)
- iOS界面布局之三——纯代码的autoLayout及布局动画 iOS8及VFL语言
- java集合框架图
- 自动化测试使用mybatis更新数据库信息实例
- 【DP/单调栈】关于单调栈的一些题目(codevs 1159,codevs 2673)
- Android设置背景色为透明
- 手机移动广告打头阵助力移动营销成功
- 数据库sql中and和or优先级的问题
- 文章标题