Basic knowledge of fragment

fragment的创建。。基本属性

动态创建

//fragment管理器
FragmentManager supportFragmentManager = getSupportFragmentManager();
//开启一个fragment
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
//第一个参数是fragment的布局id(容器),第二个是fragment对象
BlankFragment blankFragment = new BlankFragment();
fragmentTransaction.add(R.id.fragment_layout_id, blankFragment);
//显示与隐藏
fragmentTransaction.show(blankFragment);
//隐藏
fragmentTransaction.hide(blankFragment);
//显示,提交事务
fragmentTransaction.commit();

静态

在布局中创建fragment。。 
<fragment
android:id="@+id/id"
android:name="这里填写全部名字"
android:layout_width="match_parent"
android:layout_height="match_parent">

</fragment>

fragment的生命周期

在这里插入图片描述

fragment 的生命周期意思:

1.onAttach() :Fragment与Activity有联系。

2.onCreate():创建Fragment

3.onCreateView():创建Fragment视图,尽量不要做耗时操作

4.onActivityCreated():当Activity中的onCreate方法执行完后调用。

5.onStart():启动。

6.onResume():可见

7.onPause():不可见

8.onStop():停止。

9. onDestroyView() :销毁Fragment视图

10.onDestroy():销毁fragment对象

11.onDetach():Fragment和Activity解除关联的时候调用

向fragment传值

activity代码:

package com.example.day005;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.example.day005.fragment.BlankFragment;

public class MainActivity extends AppCompatActivity {

    private FragmentManager supportFragmentManager;
    private FragmentTransaction fragmentTransaction;

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

        supportFragmentManager = getSupportFragmentManager();
        fragmentTransaction = supportFragmentManager.beginTransaction();
        BlankFragment blankFragment = new BlankFragment();
        fragmentTransaction.add(R.id.fragment_layout,blankFragment);
        fragmentTransaction.commit();
    }

    public void click(View view) {
        //获取值
        EditText viewById = findViewById(R.id.edit);
        String string = viewById.getText().toString();
        //创建fragment对象
        BlankFragment blankFragment = new BlankFragment();
        //创建bundle
        Bundle bundle = new Bundle();
        bundle.putString("Key",string);
        //将bundle打包传递
        blankFragment.setArguments(bundle);

        //开始更新fragment..需要重新创建fragment
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        //开始更新
        fragmentTransaction.replace(R.id.fragment_layout,blankFragment);
        //显示
        fragmentTransaction.commit();
    }
}

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=".MainActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击发送"
        android:layout_margin="10dp"
        android:onClick="click"/>

    <FrameLayout
        android:id="@+id/fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp">

    </FrameLayout>

</LinearLayout>

fragment代码:

package com.example.day005.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.day005.R;

import java.security.Key;

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

    private TextView textView;

    public BlankFragment() {
        // 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_blank, container, false);
        textView = inflate.findViewById(R.id.fragment_text);
        //传值
        Bundle arguments = getArguments();
        if(arguments != null){
            String string = arguments.getString("Key");
            textView.setText(string);
        }
        return inflate;
    }
}

fragment布局:

<?xml version="1.0" encoding="utf-8"?>
<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=".fragment.BlankFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/fragment_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="20sp"
        android:textColor="#0f0"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值