Fragment基础知识

Fragment 介绍

答:Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,又称Activity片段!想想,如果一个很大的界面,我们 就一个布局,写起界面来会有多麻烦,而且如果组件多的话是管理起来也很麻烦!而使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在 运行过程中动态地更新Activity的用户界面!另外Fragment并不能单独使用,他需要嵌套在Activity 中使用,尽管他拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity 被destory销毁了,他也会跟着销毁!。

Fragment为什么被称为第五大组件?

Fragment的使用次数是不输于其他四大组件的,而且Fragment有自己的生命周期,比Activity更加节省内存。

Fragment的优势有以下几点:

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

Fragment 应用

如何创建Fragment

创建Fragment对象:(右击新建Fragment一步搞定)
在这里插入图片描述

加载Fragment的两种方式

静态加载

在这里插入图片描述
自动生成的java文档

/**
 * A simple {@link Fragment} subclass.
 */
public class FirstFragment extends Fragment {

    private static final String TAG = "FirstFragment";

    public FirstFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

自动生成的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:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--一定要注意的是:name属性是fragment的全限定名-->
    <fragment
        android:id="@+id/my_fragment_id"
        android:name="com.example.day004.fragment.MyFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </fragment>

</LinearLayout>

以上是静态加载内容.

动态加载

在这里插入图片描述
FragmentManager 介绍在这里插入图片描述
为了兼容的效果,我们选择getSupportFragmentManager()方法.
getFragmentManager()方法已经废弃.

1.一定要注意,如果用getSupportFragmentManager()这个方法,就整个项目全部用,
2.不要在项目里交叉使用getFragmentManager()方法.
3.否则你得请神仙来找错了.

在这里插入图片描述
在这里插入图片描述

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        得到碎片管理器
        FragmentManager supportFragmentManager = getSupportFragmentManager();
//        开启一个事务
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
//        第一个参数是布局文件的id
//        第二个参数是碎片
        fragmentTransaction.add(R.id.fl,new FirstFragment());
//        把事务提交
        fragmentTransaction.commit();
    }

add,remove,replace,hide 方法

package com.example.day004;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.day004.fragment.MyFragment;
import com.example.day004.fragment.OneFragment;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1,创建fragment的管理对象
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        //2,获取fragment的事物对象,并开启事务
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        //3,调用事务中相应的方法,来操作fragment
        //add方法参数,第一个要放入的容器(布局的Id),第二个是fragment对象
         MyFragment myFragment = new MyFragment();
        fragmentTransaction.add(R.id.main_layout_id,myFragment);
        //移除一个Fragment
        fragmentTransaction.remove(myFragment);
        OneFragment oneFragment = new OneFragment(); //实例化一个Fragment对象
        //replace(替换一个布局)执行过程是先 remove 然后在 add.
        fragmentTransaction.replace(R.id.main_layout_id,oneFragment);
        //隐藏一个Fragment
        fragmentTransaction.hide(oneFragment);
        //4,提交事务
        fragmentTransaction.commit();

    }
}

Fragment的生命周期

在这里插入图片描述
文字描述版本的生命周期.
在这里插入图片描述

Fragment 传值介绍

Activity布局文件

<?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"
    tools:context=".ShowActivity"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入点啥"
        android:id="@+id/edittext"
        android:layout_marginTop="10dp"
        android:background="@android:drawable/edit_text"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交"
        android:layout_marginTop="10dp"
        android:id="@+id/button"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/textview"
        android:layout_marginTop="10dp"></FrameLayout>

</LinearLayout>

Activity代码

public class ShowActivity extends AppCompatActivity {
    private EditText edittext;
    private Button button;
    private FrameLayout textview;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        //0,取到edit的控件和值
        edittext = findViewById(R.id.edittext);
        button = findViewById(R.id.button);
        textview = findViewById(R.id.textview);

        //1,在这里动态添加一个fragment
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        final FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        //注意这个布局文件,是R.layout.activity_a_2_f xml文件里的线性布局
        fragmentTransaction.add(R.id.textview,new ShowFragment());
        fragmentTransaction.commit();


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager supportFragmentManager1 = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction1 = supportFragmentManager1.beginTransaction();
                //创建fragment对象
                ShowFragment showFragment = new ShowFragment();
                //创建bundle
                Bundle bundle = new Bundle();
                bundle.putString("name",edittext.getText().toString().trim());
                //给fragment对象赋值
                showFragment.setArguments(bundle);
                //动态修改fragment
                fragmentTransaction1.replace(R.id.textview,showFragment);
                fragmentTransaction1.commit();
            }
        });
    }

Fragment布局文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ShowFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"
        android:id="@+id/textview1111"/>

</FrameLayout>

Fragment布局文件中添加一个id即可

Fragment代码

/**
 * A simple {@link Fragment} subclass.
 */
public class ShowFragment extends Fragment {


    public ShowFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_show, container, false);
        TextView textView = inflate.findViewById(R.id.textview1111);
        Bundle arguments = getArguments();
        if (arguments!=null){
            String name = arguments.getString("name");
            textView.setText(name);
        }
        return inflate;
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio是一款由Google开发的Android应用程序开发工具,它是基于IntelliJ IDEA的集成开发环境(IDE)。以下是Android Studio编程知识框架的概述: 1. Java基础知识:Android Studio使用Java语言进行开发,因此需要掌握Java的基础知识,例如数据类型、控制语句、面向对象编程等。 2. Android框架:Android Studio使用Android框架进行应用程序开发,需要掌握Android框架的各个组件,例如ActivityFragment、Service、BroadcastReceiver等。 3. 布局和控件:Android Studio使用XML文件进行布局设计,需要掌握各种布局和控件的使用方法,例如LinearLayout、RelativeLayout、Button、TextView等。 4. 数据存储:Android Studio应用程序需要对数据进行存储和管理,需要掌握SQLite数据库的使用方法以及SharedPreferences的使用方法。 5. 网络编程:Android Studio应用程序需要与服务器进行通信,需要掌握HTTP协议、Socket编程、JSON数据解析等知识。 6. 多媒体处理:Android Studio应用程序需要对多媒体文件进行处理,需要掌握音频和视频的播放、录制和编辑等知识。 7. 性能优化:Android Studio应用程序需要考虑性能问题,需要掌握内存管理、线程管理、UI优化等知识。 8. 调试和测试:Android Studio应用程序需要进行调试和测试,需要掌握调试工具的使用方法以及单元测试和UI测试的方法。 9. 版本控制:Android Studio应用程序需要进行版本控制,需要掌握Git等版本控制工具的使用方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值