Fragment使用詳解-創(chuàng)新互聯(lián)

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

    成都創(chuàng)新互聯(lián)專注于遼陽(yáng)網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供遼陽(yáng)營(yíng)銷型網(wǎng)站建設(shè),遼陽(yáng)網(wǎng)站制作、遼陽(yáng)網(wǎng)頁(yè)設(shè)計(jì)、遼陽(yáng)網(wǎng)站官網(wǎng)定制、微信小程序開(kāi)發(fā)服務(wù),打造遼陽(yáng)網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供遼陽(yáng)網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
    • Fragment必須依附Activity而存在,因此Fragment的生命周期和Activity極為相識(shí),但是又有自己獨(dú)特的生命周期回調(diào)。
    • Fragment正常情況下從創(chuàng)建到銷毀的生命周期回調(diào):onAttach(依附于宿主activity),onCreate(系統(tǒng)創(chuàng)建Fragment),onCreateView(創(chuàng)建布局文件),onActivityCreated(activity 的onCreate回調(diào)后會(huì)調(diào)用該生命周期方法),onStart(),onResume(),onPause(),onStop(),onDestroyView(),onDestroy(),onDetach()
      Fragment使用詳解

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

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

      2.?將手機(jī)屏幕向上,旋轉(zhuǎn)180度,不會(huì)觸發(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);//第一個(gè)參數(shù)是 ViewGroup,即應(yīng)該放置片段的位置,由資源 ID 指定,第二個(gè)參數(shù)是要添加的片段。
    fragmentTransaction.commit();

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

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

      // 實(shí)現(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;
      }
    // 實(shí)現(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="測(cè)試。。。。巴拉巴拉巴拉巴拉巴拉。....巴拉。"
        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)的時(shí)候會(huì)拋出異常,但是DialogFragment不會(huì)拋出異常信息。下面是使用普通的Dialog在屏幕旋轉(zhuǎn)時(shí)發(fā)生的異常信息:
    Fragment使用詳解

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

名稱欄目:Fragment使用詳解-創(chuàng)新互聯(lián)
文章URL:http://bm7419.com/article2/dscsic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)公司、定制開(kāi)發(fā)、搜索引擎優(yōu)化移動(dòng)網(wǎng)站建設(shè)

廣告

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

成都app開(kāi)發(fā)公司