• Android-------------fragment的学习


    一、fragment的静态使用

      1.使用步骤:  

         a.继承fragment   重写onCreatevVew()的回调方法 

       b.设置Fragment的布局

       c.在Activity中声明Fragment  使用方式与控件相同

      2.代码展示:

     a.继承fragment   重写onCreatevVew()的回调方法
    public class TileFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.tilefragment, null);
    }
    }
    b.设置Fragment的布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"> //忘记写会有控件覆盖,会报错的

    <ImageView
    android:id="@+id/iv_bck"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:src="@mipmap/ic_launcher" />

    <TextView
    android:gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="我是标题"
    />
    </LinearLayout>


    c.在Activity中声明Fragment  使用方式与控件相同
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" //这个忘写会报错误,很难找出来的
    tools:context=".MainActivity">

    <fragment
    android:name="com.example.zhang.myapplication.TileFragment" ----------->引用标签来静态加载fragment的数据
    android:layout_width="match_parent"
    android:layout_height="50dp"></fragment>
    </LinearLayout>

    二、fragment的动态使用
      a.创建Fragment的管理器对象
      b.获取Fragment的事物对象并开启
      c.调用事物的动态方法:动态的添加、动态的删除、动态的替换
      d.提交事物
      
      在xml文件中的代码,在布局文件中写好布局并设置ID,通过FragmentTransation.add去动态添加
      <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/fragment_tile"
    />
      <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frag_content"
    />
      //获得FragmentManager的对象
      FragmentManager manager = getSupportFragmentManager();
      //获取Fragment的事物对象并开始
      FragmentTransaction transaction = manager.beginTransaction();
      //动态添加fragment (add remove replace)
      transaction.add(R.id.fragment_tile, new TileFragment());
      transaction.add(R.id.frag_content, new ContentFragment());
      //提交获得的事物
      transaction.commit();

    三、Fragment的生命周期
      1.第一次启动app的时候
        onAttach:activity与fragment的产生关联时候进行回调
        onCreateView : 用户第一次绘制界面
        onActivityCreate : fragment与activity创建成功进行回调

            

      2.切换其他fragment的时候

      3.返回第一次启动的fragment的时候

      4.按home键的时候

      5.home键之后再重新返回当前界面

      6.按back键的时候

      7.fragment和activity的生命周期

         

       





      

      

  • 相关阅读:
    引擎设计跟踪(九.10) Max插件更新,地形问题备忘
    引擎设计跟踪(九.9) 文件包系统(Game Package System)
    [原] Android上使用native IO
    [原] GLES在iOS和Android上的不同
    [原] perforce 获取本地最近更新的Changelist
    [转]GLES 3.0 新特性
    [原]android不支持命名的semaphore
    [原]android 链接错误
    引擎设计跟踪(九.8) Gizmo helper实现与多国语言
    [原]游戏引擎与游戏逻辑
  • 原文地址:https://www.cnblogs.com/liunx1109/p/9867764.html
Copyright © 2020-2023  润新知