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();
            */
        }
    }
    
    
    • 项目运行

在这里插入图片描述

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