Android Fragment 基本概念和基本使用

ゝ一世哀愁。 2024-02-21 07:32 122阅读 0赞

Android Fragment 基本概念和基本使用

一、基本概念

Fragment,简称碎片,是Android 3.0(API 11)提出的,为了兼容低版本,support-v4库中也开发了一套Fragment API,最低兼容Android 1.6。

过去support-v4库是一个jar包,24.2.0版本开始,将support-v4库模块化为多个jar包,包含:support-fragment, support-ui, support-media-compat等,这么做是为了减少APK包大小,你需要用哪个模块就引入哪个模块。

Android 官方对Fragment的解释如下:

A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running.

Fragment表示Activity中的行为或user interface的一部分。可以在单个Activity中组合多个Fragment来构建多窗格 UI 并在多个活动中复用Fragment。可以将Fragment视为Activity的模块化部分,它具有自己的生命周期,接收自己的输入事件,并且可以在Activity运行时添加或删除

由此可知

  • Fragment是依赖于Activity的,不能独立存在的。
  • 一个Activity里可以有多个Fragment。
  • 一个Fragment可以被多个Activity重用。
  • Fragment有自己的生命周期,并能接收输入事件。
  • 我们能在Activity运行时动态地添加或删除Fragment。

Fragment的优势有以下几点:

  • 模块化(Modularity):我们不必把所有代码全部写在Activity中,而是把代码写在各自的Fragment中。
  • 可重用(Reusability):多个Activity可以重用一个Fragment。
  • 可适配(Adaptability):根据硬件的屏幕尺寸、屏幕方向,能够方便地实现不同的布局,这样用户体验更好。

image

Fragment核心的类有:

  • Fragment:Fragment的基类,任何创建的Fragment都需要继承该类。
  • FragmentManager:管理和维护Fragment。他是抽象类,具体的实现类是FragmentManagerImpl。
  • FragmentTransaction:对Fragment的添加、删除等操作都需要通过事务方式进行。他是抽象类,具体的实现类是BackStackRecord。

二、基本使用

在res/layout目录下,新建fragment1.xml,其中内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#FF00FF">
  6. <TextView
  7. android:id="@+id/fragment1_text"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="this is fragment1"
  11. android:textColor="#000000"
  12. android:textSize="25sp"/>
  13. </LinearLayout>

在res/layout目录下,新建fragment2.xml,其中内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#FFFF00">
  6. <TextView
  7. android:id="@+id/fragment2_text"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="this is fragment2"
  11. android:textColor="#000000"
  12. android:textSize="25sp" />
  13. </LinearLayout>

在res/layout目录下,activity_main.xml内容如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <fragment
  9. android:id="@+id/fragment1"
  10. android:name="com.example.fragmentdemo.FragmentOne"
  11. android:layout_width="0dip"
  12. android:layout_height="match_parent"
  13. android:layout_weight="1" />
  14. <fragment
  15. android:id="@+id/fragment2"
  16. android:name="com.example.fragmentdemo.FragmentTwo"
  17. android:layout_width="0dip"
  18. android:layout_height="match_parent"
  19. android:layout_weight="1" />
  20. </LinearLayout>

新建的FragmentOne类,继承Fragment,在onCreateView里通过inflate(R.layout.fragment1, container, false)加载刚才写过的fragment1.xml里的布局

  1. package com.example.fragmentdemo;
  2. import android.app.Activity;
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. public class FragmentOne extends Fragment {
  10. public static final String TAG = "Fragment1";
  11. @Override
  12. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  13. Log.d(TAG, "onCreateView");
  14. return inflater.inflate(R.layout.fragment1, container, false);
  15. }
  16. }

新建的FragmentTwo类,继承Fragment,在onCreateView里通过inflate(R.layout.fragment1, container, false)加载刚才写过的fragment2.xml里的布局

  1. package com.example.fragmentdemo;
  2. import android.app.Activity;
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12. public class FragmentTwo extends Fragment {
  13. public static final String TAG = "Fragment2";
  14. @Override
  15. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
  16. return inflater.inflate(R.layout.fragment2, container, false);
  17. }
  18. }

MainActivity内容如下:

  1. package com.example.fragmentdemo;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.fragment.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.Display;
  6. public class MainActivity extends AppCompatActivity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. }
  12. }

整体的目录结构如下:

image-20231016192023317

显示出来的效果如下所示:

Screenshot\_2023-10-16-19-36-46-85\_8edc095a2913769c806222abc63c77d4

其中如果一个Activity里这种分割开来的片段较多,即可采取多个fragment来组成。

三、Fragment之间如何通信

getActivity方法可以让Fragment获取到关联的Activity,然后再调用Activity的findViewById方法,就可以获取到和这个Activity关联的其它Fragment的视图了。

比如上文中的第二个Fragement需要获取第一个Fragment的文本内容的话,就可以通过

在onActivityCreated方法中,这样来获取

  1. @Override
  2. public void onActivityCreated(Bundle savedInstanceState) {
  3. super.onActivityCreated(savedInstanceState);
  4. TextView textView1= getActivity().findViewById(R.id.fragment1_text);
  5. TextView textView2 = getActivity().findViewById(R.id.fragment2_text);
  6. // 设置第二个Fragement的文本为第一个Fragment的文本
  7. textView2.setText(textView1.getText());
  8. Log.d(TAG, "onActivityCreated");
  9. }

Screenshot\_2023-10-16-19-46-05-20\_8edc095a2913769c806222abc63c77d4

参考:https://blog.csdn.net/guolin\_blog/article/details/8881711

发表评论

表情:
评论列表 (有 0 条评论,122人围观)

还没有评论,来说两句吧...

相关阅读

    相关 jcs-基本概念使用

    前言 如果一个计算非常耗时,每次获取都重新计算,会消耗很多额外的计算资源。对于这些耗时的计算结果,可以存储在缓存中,下次需要的时候,直接访问缓存中的内容,而不是再重新计算