Fragment初识
来源:互联网 发布:js注册界面 编辑:程序博客网 时间:2024/06/06 00:55
Fragment
1. 历史背景
适应平板而推出的系统,与苹果Ipad进行竞争。
2.注意事项:
2.1 Fragment的使用包一般为android.support.v4.app.Fragment2.2 FragmentActivity 的使用包一般为android.support.v4.app.FragmentActivity,在该活动中 获取碎片管理: getSupportFragmentManager() 若viewpager 定义适配器,需继承FragmentPagerAdapter2.3 Fragment常与Activity 进行关联使用,生命周期与Activity的生命周期息息相关,可以片面的理解为轻量级的Activity.
3. 使用
第一种:
3.1 在xml 中定义 布局文件 res-->layout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0" android:src="@drawable/m1" /> </LinearLayout>3.2 创建 Fragment类 继承V4包下的Fragment,重新onCreateView,将视图加载到 碎片中。 public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, null); return view; } }3.3 在Activity 中使用碎片 直接在xml 中定义 fragment ,可以看到 可以通过name反射,找到fragment,就将Fragment1 中的视图View加载。 <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:orientation="horizontal" > <fragment android:name="com.example.fragmet_staticdemo.Fragment1" //采用反射机制 android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout> 通过 onCreat()方法中 加载视图 setContentView(R.layout.activity_main);
第二种:
3.1 在xml 中定义 布局文件 res-->layout Fragment1.xml 中 <?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="#00ff00" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是第一个fragment" /> </LinearLayout> activity_main.xml 中 <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="showSecond" android:text="展示第二个fragment" /> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> 3.2 创建 Fragment类 继承V4包下的Fragment,重新onCreateView,将视图加载到 碎片中。 public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // true 说明我们的这个布局要加到root的根布局下边去, // false 说明只做返回xml布局 return inflater.inflate(R.layout.fragment1, container, false); } } 3.3 在Activity 中使用碎片 动态处理: 1.获取Fragment对象; 2.获取FragmentManager 容器,对象; 3.通过容器 .beginTransaction() 获取事务对象 FragmentTransaction 4.事务添加 视图id和碎片对象; 将碎片对象中视图 放入 该视图中。一般为 帧布局 5.事务的提交,进行展示。 点击button展示碎片。 public void showFirst(View v) { // 思考:想要展示Fragment1,创建一个对象 Fragment1 fragment1 = new Fragment1(); // 需要一个容器来放fragment1 // 怎样到容器里边去 // getFragmentManager(); FragmentManager supportFragmentManager = getSupportFragmentManager(); // 看一下,有没有一个方法,将我们的Fragment展示到容器上 FragmentTransaction beginTransaction = supportFragmentManager .beginTransaction(); //就看看事务上边有没有add ??? beginTransaction.add(R.id.frameLayout, fragment1); //事务提交 beginTransaction.commit(); }
4. 生命周期
比Acitivity 多出来的几个生命周期方法;
onAttach(Activity):当Fragment与Activity发生关联时调用。onCreateView(LayoutInflater, ViewGroup,Bundle):创建该Fragment的视图onActivityCreated(Bundle):当Activity的onCreate方法执行后调用onDestoryView():与onCreateView相对应,当该Fragment的视图被移除时调用onDetach():与onAttach相对应,当Fragment与Activity关联被取消时调用
最常用的方法:
onCreateView(LayoutInflater, ViewGroup,Bundle):创建该Fragment的视图 在这里调用Activity的方法会报错。onActivityCreated(Bundle):当Activity的onCreate方法执行后调用 在这里完整调用 Activity 的方法。
0 0
- Fragment初识
- Fragment初识
- 初识Fragment
- Fragment初识
- Fragment初识
- 初识Fragment
- Fragment初识
- Learning--初识Fragment Bundles
- Fragment的生命周期初识
- Android 初识Fragment
- Fragment初识(一)
- Fragment初识(二)
- Fragment初识(三)
- Fragment初识(三)
- Fragment初识(四)
- Fragment初识之静态显示
- 初识android——Fragment
- Fragment初识之Fragment管理器(管理多个Fragment)
- 使用sizeof计算虚拟继承的类对象的空间大小
- POI读取excel文件
- 【已解决】遭遇MultiDex
- 打印int值的二进制形式
- C++“准”标准库Boost学习指南(12):Boost.Signals
- Fragment初识
- 数据库的子表和父表最简单区别方法
- 策略模式详解
- BZOJ 1855 [Scoi2010]股票交易
- Cloudera 从5.2.0升级到最新5.8.2的官方文档翻译
- Android 自定义View之边缘凹凸的优惠券效果
- Jenkins中svn地址有多个,SVN_URL失效的解决办法
- ECMAScript6语法介绍及编程风格
- C语言产生随机数的方法