成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Fragment使用詳解

  1. Fragment概述:
    • Fragment為片段,在Android3.0(api:11)的時候加入,早期是為了大屏幕(如平板)而設(shè)計的。因為平板要比手機的屏幕大的多,在UI設(shè)計方面會留有比手機大的多的空間,利用片段來實現(xiàn)UI設(shè)計,可以將UI分隔成多個不同的模塊,即可以實現(xiàn)復(fù)雜的UI設(shè)計,又可以實現(xiàn)復(fù)用,并且可以在Android運行時動態(tài)的添加和刪除片段,對開發(fā)提供了極大的便利。
  2. Fragment的生命周期:

    成都創(chuàng)新互聯(lián)公司服務(wù)項目包括祥符網(wǎng)站建設(shè)、祥符網(wǎng)站制作、祥符網(wǎng)頁制作以及祥符網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,祥符網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到祥符省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

    • Fragment必須依附Activity而存在,因此Fragment的生命周期和Activity極為相識,但是又有自己獨特的生命周期回調(diào)。
    • Fragment正常情況下從創(chuàng)建到銷毀的生命周期回調(diào):onAttach(依附于宿主activity),onCreate(系統(tǒng)創(chuàng)建Fragment),onCreateView(創(chuàng)建布局文件),onActivityCreated(activity 的onCreate回調(diào)后會調(diào)用該生命周期方法),onStart(),onResume(),onPause(),onStop(),onDestroyView(),onDestroy(),onDetach()
      Fragment使用詳解

    • 旋轉(zhuǎn)屏幕時的生命周期:

      1. 無論是從豎屏轉(zhuǎn)向橫屏還是橫屏轉(zhuǎn)豎屏,都是一個正常的銷毀重建流程,生命周期的回調(diào)為:onPause(),onStop,onDestroyVie,onDestroy(),onDetach(),onAttach(),onCreate(),onCreateView(),onActivityCreated(),onStart(),onResume()。
        Fragment使用詳解

      2.?將手機屏幕向上,旋轉(zhuǎn)180度,不會觸發(fā)任何生命周期。

  3. 創(chuàng)建界面:
    • 拓展Fragment,并在onCreateView中添加相應(yīng)的布局。
  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);//第一個參數(shù)是 ViewGroup,即應(yīng)該放置片段的位置,由資源 ID 指定,第二個參數(shù)是要添加的片段。
    fragmentTransaction.commit();

  5. 注意事項:
    1. 一個FragmentTransaction只能執(zhí)行一次
    2. 相同的Fragment不能被add到同一個Fragment
    3. 容器未移除視圖就add新的Fragment會發(fā)生內(nèi)容重疊
  6. DialogFragment:

    1. DialogFragment是在Android3.0以后引入的一種特殊的Fragment,官方推薦使用DialogFragment,原因在于:DialogFragment與Fragment有著相同的生命周期,便于管理生命周期,DialogFragment也可以實現(xiàn)重用,另外DialogFragment可以有普通Dialog沒有優(yōu)勢,比如可以防止窗體泄露,具體情況下面的 window Leak。
    2. 拓展DialogFragment需要實現(xiàn)onCreateView或者onCreateDialog:

      // 實現(xiàn)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;
      }
    // 實現(xiàn)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在屏幕旋轉(zhuǎn)的時候會拋出異常,但是DialogFragment不會拋出異常信息。下面是使用普通的Dialog在屏幕旋轉(zhuǎn)時發(fā)生的異常信息:
    Fragment使用詳解

分享標題:Fragment使用詳解
鏈接分享:http://jinyejixie.com/article22/ijgjjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、小程序開發(fā)、品牌網(wǎng)站設(shè)計、企業(yè)建站、網(wǎng)站設(shè)計、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計
汉川市| 迭部县| 莒南县| 济宁市| 平遥县| 青龙| 双鸭山市| 博兴县| 安陆市| 南昌市| 平泉县| 陆川县| 十堰市| 延庆县| 永川市| 塔河县| 株洲县| 金塔县| 当涂县| 钟山县| 嘉义县| 朝阳县| 兰考县| 恩平市| 丹凤县| 灌南县| 双牌县| 三亚市| 阿克苏市| 宣城市| 郯城县| 大新县| 德昌县| 陆良县| 太白县| 佛冈县| 贵溪市| 务川| 基隆市| 格尔木市| 昂仁县|