Frament

Fragment在Android中的应用

Frament的介绍

    可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。

  可以把Fragment设计成可以在多个Activity中复用的模块。

Frament的生命周期

因为Fragment必须嵌入在Acitivity中使用,所以Fragment的生命周期和它所在的Activity是密切相关的。

  如果Activity是暂停状态,其中所有的Fragment都是暂停状态;如果Activity是stopped状态,这个Activity中所有的Fragment都不能被启动;如果Activity被销毁,那么它其中的所有Fragment都会被销毁。

  但是,当Activity在活动状态,可以独立控制Fragment的状态,比如加上或者移除Fragment。

<?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"
    android:orientation="horizontal"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.android_fragment.MainActivity">

   <fragment
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/res_left"
    android:name="com.example.android_fragment.fragmentLeft"
    ></fragment>
    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:id="@+id/f_main_right"
        android:name="com.example.android_fragment.fragmentRigth"
        ></fragment>
</LinearLayout>
把这个View一分为二


 android:name="com.example.android_fragment.fragmentLeft"
这句代码是引用了一个View

<?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="com.example.android_fragment.fragmentLeft">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/res_left_phone"
        ></ListView>
</LinearLayout>
 android:name="com.example.android_fragment.fragmentRigth"
<?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="com.example.android_fragment.fragmentRigth">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/res_rigth_phone"
    ></TextView>
</LinearLayout>
最终结果是这样的你选择谁就调用谁的界面


代码如下:

       选择一个的用时就会调用一个View视图,我在这里简单的写了四个

package com.example.android_fragment;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class fragmentLeft extends Fragment {

    private ListView res_left_phone;
    private String[] list={"黄1","黄2","黄3","黄4"};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_fragment_left,null);
        res_left_phone = (ListView) view.findViewById(R.id.res_left_phone);
        ArrayAdapter aa=new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,list);
        res_left_phone.setAdapter(aa);
        res_left_phone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //情况:右边是不同的布局
                //获取碎片的管理者
               FragmentManager fm=getFragmentManager();
                //获取事务
                FragmentTransaction ft=fm.beginTransaction();

                switch (position) {
                    case 0:
                        WjFragment wjFragment=new WjFragment();//这是new了自己写的和他相对应的View视图
                        //替换
                        ft.replace(R.id.f_main_right,wjFragment) ;
                        break;
                    case 1:
                        BgFragment bgFragment=new BgFragment();//这是new了自己写的和他相对应的View视图

                        ft.replace(R.id.f_main_right,bgFragment) ;
                        break;
                    case 2:
                        XxFragment xxFragment=new XxFragment();//这是new了自己写的和他相对应的View视图

                        ft.replace(R.id.f_main_right,xxFragment) ;
                        break;
                    case 3:
                        SmallSisterFragment smallSisterFragment=new SmallSisterFragment();//这是new了自己写的和他相对应的View视图

                        ft.replace(R.id.f_main_right,smallSisterFragment) ;
                        break;
                }
                ft.commit();
            }
        });
        return view;
    }

}


### 定义 Fragment 意思是“碎片、片段”,表示 Activity 中的行为或用户界面部分,是 Activity 的模块化组成部分,可将多个片段组合在一个 Activity 中来构建多窗格 UI,也能在多个 Activity 中重复使用某个片段。它具有自己的生命周期,能接收自己的输入事件,可在 Activity 运行时添加或移除,可当作“子 Activity”在不同 Activity 中复用。Fragment 作为 Activity 界面的一部分必须依附于 Activity,与 Activity 一样拥有自己的生命周期,还能处理用户的交互动作。同一个 Activity 可以有一个或多个 Fragment 作为界面内容,可动态添加、删除 Fragment,灵活控制 UI 内容,也可用于解决部分屏幕适配问题 [^2][^4][^5]。 ### 使用方法 #### 静态使用 将 Fragment 当成普通的 View 一样声明在 Activity 的布局中,事件处理交给 Fragment。步骤如下: 1. 在 Activity 布局中添加 fragment 控件,用于加载想要显现的视图。 2. 创建 Fragment 类继承 Fragment,重写其中的 onCreateView(),使用 inflate 加载 Fragment 布局并返回视图 [^2]。 #### 动态使用 动态加载时,因有许多完成特定功能的 Fragment,所以需要对其进行管理,这时用到 FragmentManage 类。对 Fragment 进行添加、移除、替换等操作需通过 Fragment 事务处理,用到 FragmentTransaction 类。步骤如下: 通过 FragmentManager.beginTransaction() 开始一个事务,可在事务中对 Fragment 进行操作,如添加 add()、移除 remove()、替换 replace() 等,最后提交事务 commit(),还有 addToBackStack()、attach() 等操作。注意,Fragment 通过 ID 或者 Tag 作为标识,对 Fragment 操作后一定要提交,即调用 commit() [^2]。 ### 应用场景 Fragment 的目的是为了解决在不同的显示屏上动态和灵活的 UI 设计。可用于构建多窗格 UI,在同一个 Activity 中组合多个片段;可动态添加、删除 Fragment,灵活控制 UI 内容;还能用来解决部分屏幕适配问题 [^2][^4]。 ### 代码示例 以下是自定义 MyFragmentPagerAdapter 继承自 FragmentPagerAdapter 的示例代码: ```java import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; public class MyFragmentPagerAdapter extends FragmentPagerAdapter { public MyFragmentPagerAdapter(FragmentManager fm) { super(fm); } // 滑动的过程中呈现的Fragment的确定 @Override public Fragment getItem(int arg0) { Fragment fragment = null; switch (arg0) { case 0: fragment = new FragmnetFirst(); break; case 1: fragment = new FragmentSecond(); break; case 2: fragment = new FragmentThird(); break; } return fragment; } // 滑动的过程中可以呈现Fragment的个数 @Override public int getCount() { return 3; } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值