Android|Fragment的入門使用

Fragment是什麼

  • Fragment 依賴於 Activity 存在。它能夠幫助 Activity 完成部分佈局工作。例如下面一塊手機屏幕,其中的矩形區域的佈局顯示就可以交由 Fragment 處理,其餘部分交給 Activity 處理。

在這裏插入圖片描述

  • 在一個 Activity 中可以動態的添加,替換,移除不同的 Fragment,因此對於信息的顯示有很大的便利性。
  • Fragment 的優點:
    • 模塊化(Modularity):代碼不必全部寫在 Activity 中,可以將任務分發到多個 Fragment中。
    • 可重用(Reusablity):同一個 Fragment 可以被多個 Activity 使用。
    • 可適配(Adaptablity):根據硬件的屏幕尺寸、屏幕方向,能夠方便地實現不同的佈局,用戶體驗更好。
  • 雖然Fragment 依附於 Activity,但Fragment 擁有自己的生命週期,可以處理用戶的事件。

使用步驟

  1. 爲 Fragment 部分單獨編寫一個佈局文件。
  2. 編寫自己的Fragment類,此類完成獲取佈局對象,填充控件等任務。
  3. 將 Fragment 填充到 Activity 中。
  • 首先我們要有一個 Activity,這個 Activity 中留出一部分交給 Fragment 完成佈局。

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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 -->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:id="@+id/fl"/>
        
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/btn_change"
            android:text="按鈕"
            android:textSize="20sp"/>
    </LinearLayout>
    

在這裏插入圖片描述

  • 接下來我們來編寫 fragment。首先這個 fragment 要有自己獨立的一個佈局文件。這裏佈局文件決定了我們如何去填充 Activity 中留出的那塊區域。

    myfragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="0dp"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:src="@drawable/pkq1"
                android:scaleType="centerCrop"/>
    
            <ImageView
                android:layout_width="0dp"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:src="@drawable/pkq1"
                android:scaleType="centerCrop"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:src="@drawable/pkq1"
                android:scaleType="centerCrop"/>
        </LinearLayout>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="200sp"
            android:text="來自皮卡丘的凝視"
            android:textSize="30sp"
            android:gravity="center"/>
    
    </LinearLayout>
    

在這裏插入圖片描述

  • 接下要編寫自己的Fragment 類了,這個類需要繼承 androidx.fragment.app.Fragment 類。

    public class FirstFragment extends Fragment {
    
        //創建該Fragment的視圖
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            //注意使用inflater構建View時一定要將attachToRoot指明false,因爲Fragment會自動將視圖添加到container中,attachToRoot爲true會重複添加報錯。
            View v = inflater.inflate(R.layout.myfragment,container,false);
            return v;
        }
    }
    
  • 最後就是 Activity 中的代碼了,需要把編寫好的 Fragment 添加進去。

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //獲取Fragment管理器
            FragmentManager manager = getSupportFragmentManager();
            //獲取Fragment事務
            FragmentTransaction transaction = manager.beginTransaction();
            //add (int containerViewId,Fragment fragment)
            // 填充fragment到activity_main中的FrameLayout控件
            transaction.add(R.id.fl, new FirstFragment());
            //提交
            transaction.commit();
            
            //鏈式編程
            /*
            getSupportFragmentManager().beginTransaction().add(R.id.fl, new FirstFragment()).commit();
            */
        }
    }
    
    
    • 項目運行

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章