Fragment使用详解

  1. Fragment概述:
    • Fragment为片段,在Android3.0(api:11)的时候加入,早期是为了大屏幕(如平板)而设计的。因为平板要比手机的屏幕大的多,在UI设计方面会留有比手机大的多的空间,利用片段来实现UI设计,可以将UI分隔成多个不同的模块,即可以实现复杂的UI设计,又可以实现复用,并且可以在Android运行时动态的添加和删除片段,对开发提供了极大的便利。
  2. Fragment的生命周期:

    成都创新互联公司服务项目包括祥符网站建设、祥符网站制作、祥符网页制作以及祥符网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,祥符网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到祥符省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

    • Fragment必须依附Activity而存在,因此Fragment的生命周期和Activity极为相识,但是又有自己独特的生命周期回调。
    • Fragment正常情况下从创建到销毁的生命周期回调:onAttach(依附于宿主activity),onCreate(系统创建Fragment),onCreateView(创建布局文件),onActivityCreated(activity 的onCreate回调后会调用该生命周期方法),onStart(),onResume(),onPause(),onStop(),onDestroyView(),onDestroy(),onDetach()
      Fragment使用详解

    • 旋转屏幕时的生命周期:

      1. 无论是从竖屏转向横屏还是横屏转竖屏,都是一个正常的销毁重建流程,生命周期的回调为:onPause(),onStop,onDestroyVie,onDestroy(),onDetach(),onAttach(),onCreate(),onCreateView(),onActivityCreated(),onStart(),onResume()。
        Fragment使用详解

      2.将手机屏幕向上,旋转180度,不会触发任何生命周期。

  3. 创建界面:
    • 拓展Fragment,并在onCreateView中添加相应的布局。
  4. 添加到Activity中:

    • 在布局文件中使用fragment属性:
      <fragment
          android:id="@+id/one_fragment"
          android:name="com.cy.test.fragmentapplication.OneFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>
    • 在Java代码中添加:
      
      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    ExampleFragment fragment = new ExampleFragment();
    fragmentTransaction.add(R.id.fragment_container, fragment);//第一个参数是 ViewGroup,即应该放置片段的位置,由资源 ID 指定,第二个参数是要添加的片段。
    fragmentTransaction.commit();

  5. 注意事项:
    1. 一个FragmentTransaction只能执行一次
    2. 相同的Fragment不能被add到同一个Fragment
    3. 容器未移除视图就add新的Fragment会发生内容重叠
  6. DialogFragment:

    1. DialogFragment是在Android3.0以后引入的一种特殊的Fragment,官方推荐使用DialogFragment,原因在于:DialogFragment与Fragment有着相同的生命周期,便于管理生命周期,DialogFragment也可以实现重用,另外DialogFragment可以有普通Dialog没有优势,比如可以防止窗体泄露,具体情况下面的 window Leak。
    2. 拓展DialogFragment需要实现onCreateView或者onCreateDialog:

      // 实现onCreateView 
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState) {
      
      View inflate = inflater.inflate(R.layout.dialog_fragment_test1, container);
      return inflate;
      }
    // 实现onCreateDialog
        @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        LayoutInflater inflater = Objects.requireNonNull(getActivity()).getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.dialog_fragment_test1, null));
        return builder.create();
    }
    <!-- dialog_fragment_test1的布局文件 -->
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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">
    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="50dp"
        android:layout_marginTop="4dp"
        android:text="测试。。。。巴拉巴拉巴拉巴拉巴拉。....巴拉。"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />
    
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
    
    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
    
    </android.support.constraint.ConstraintLayout>
  7. 普通的Dialog在屏幕旋转的时候会抛出异常,但是DialogFragment不会抛出异常信息。下面是使用普通的Dialog在屏幕旋转时发生的异常信息:
    Fragment使用详解

分享标题:Fragment使用详解
链接分享:https://www.cdcxhl.com/article22/ijgjjc.html

成都网站建设公司_创新互联,为您提供移动网站建设小程序开发品牌网站设计企业建站网站设计网站营销

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站网页设计