zoukankan      html  css  js  c++  java
  • 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的生命周期

         

       





      

      

  • 相关阅读:
    笨办法学Python——学习笔记4
    意识、语言、文字和程序感想
    笨办法学Python——学习笔记3
    把vim作为shell ide
    HDUYuna's confusion 树状数组 Or Multiset
    POJ3252 Round Numbers 组合数学
    HDU3874 Necklace 树状数组+离线处理
    UVA10212 The Last Nonzero Digit. 分解质因子+容斥定理
    HDU1041 Computer Transformation 大数
    HDUFish买电脑 二分查找
  • 原文地址:https://www.cnblogs.com/liunx1109/p/9867764.html
Copyright © 2011-2022 走看看